user3831093
user3831093

Reputation:

Attempting to set the layout to BoxLayout

I can't seem to find a solution online for why I'm getting this error on attempted run

I'm working on making a simple test system for a different program when are button press will yield value in a text box. I would like them to be on different lines to make it cleaner, so I looked into layouts. I decided a Box Layout would fit me best. I looked at different examples before attempting this and my code ended up looking like this, (apologies for the messy code)

Update

Got the box layout error to disappear but the code will not center them on the panel/frame. The label and button align left while the textfield becomes very large. I don't need it todo that

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import static javax.swing.BoxLayout.Y_AXIS;
import static javax.swing.SwingConstants.CENTER;

public class button extends JFrame {
static JFrame f;
static JButton b;
static JLabel l;

// main class
public static void main(String[] args)
{
    // create a new frame to stor text field and button
    f = new JFrame("panel");
    BoxLayout layout = new BoxLayout(f, BoxLayout.Y_AXIS);
    f.setLayout(layout);

    // create a label to display text
    l = new JLabel("panel label");
    b = new JButton("button1");
    JTextField textArea = new JTextField(5);
    textArea.setEditable(false);
    //textArea.append("Hello World");


    // create a panel to add buttons
    JPanel p = new JPanel();

    // add buttons and textfield to panel
    f.add(p);
    f.setSize(300, 300);
    p.add(l);
    p.add(b);
    p.setBackground(Color.white);
    p.add(textArea);
    f.show();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            Random r = new Random();
            textArea.setText(String.valueOf(r));

        }
    });
}
}






Error
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at java.desktop/javax.swing.BoxLayout.checkContainer(BoxLayout.java:461)
at java.desktop/javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:245)
at java.desktop/javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:278)
at java.desktop/java.awt.Container.addImpl(Container.java:1152)
at java.desktop/java.awt.Container.add(Container.java:1029)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553)
at java.desktop/java.awt.Container.add(Container.java:436)
at button.main(button.java:36)

I would like the three items to all to be stacked one on top of another with a space between them. The order doesn't matter right now.

Upvotes: 1

Views: 929

Answers (1)

Abra
Abra

Reputation: 20914

Swing was first added to the JDK in 1998 and has undergone a lot of changes since. Unfortunately, when you read Web pages about Swing, it is not obvious when that page was last updated. Consequently you may be learning outdated techniques for writing Swing code.

First of all, according to the code you posted, class button does not need to extend class JFrame since you use a static variable as your application's JFrame. Also, JFrame is a top-level container which makes it a special kind of container and not the same kind of continer as a JPanel. You need to set the layout manager for your JPanel and then add the JLabel, JTextField and JButton to that JPanel. And then add the JPanel to the JFrame.

Calling method pack() of class JFrame will automatically set the preferred sizes for the components inside the JFrame. It appears in the code below.

Please also look at Java coding conventions which allows others to more easily read and understand your code. And note that, according to these conventions, I renamed your class from button to Buttons and also because there are already several class in the JDK named Button.

Here is my rewrite of your code...

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Buttons implements Runnable {

    public void run() {
        createAndShowGui();
    }

    private void createAndShowGui() {
        JFrame f = new JFrame("Box");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        BoxLayout layout = new BoxLayout(p, BoxLayout.Y_AXIS);
        p.setLayout(layout);
        JLabel l = new JLabel("panel label");
        JTextField textField = new JTextField(5);
        JButton b = new JButton("button1");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Random r = new Random();
                textField.setText(String.valueOf(r.nextBoolean()));
            }
        });
        p.add(l);
        p.add(textField);
        p.add(b);
        f.add(p);
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Buttons instance = new Buttons();
        EventQueue.invokeLater(instance);
    }
}

Upvotes: 3

Related Questions