java.awt.AWTERROR When Creating a BoxLayout - java

I am trying to make a boxlayout filled with a Jlabel and 3 radio buttons in descending order. The program compiles fine but then errors out with the error BoxLayout cant be shared. I have seen people saying this error is because they are trying to attach it to a jframe, but in this case it is the jpanel being given the layout not a frame. This is the segment of code that compiles the window.
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel effortButtons = new JPanel();
JPanel skillButtons = new JPanel();
effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
skillButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
effortButtons.add(effortHeader);//this is what gives the error
effortButtons.add(oneEffort);
effortButtons.add(twoEffort);
effortButtons.add(threeEffort);
skillButtons.add(skillHeader);
skillButtons.add(oneSkill);
skillButtons.add(twoSkill);
skillButtons.add(threeSkill);
mainPanel.add(effortButtons, BorderLayout.WEST);
mainPanel.add(skillButtons, BorderLayout.EAST);
mainPanel.add(studentName, BorderLayout.NORTH);
mainPanel.add(next, BorderLayout.SOUTH);
add(mainPanel);
pack();

// xxxxxxxxxxxxx xxxxxxxxx
effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
You have to pass into the BoxLayout constructor the component that is getting the layout. So this should be:
effortButtons.setLayout(new BoxLayout(efforButtons, BoxLayout.Y_AXIS));
Likewise for our other JPanel -- change it to:
skillButtons.setLayout(new BoxLayout(skillButtons, BoxLayout.Y_AXIS));
Per the BoxLayout API:
public BoxLayout(Container target, int axis)
target - the container that needs to be laid out

Related

Implementing BoxLayout in Java Swing [duplicate]

I have this Java JFrame class, in which I want to use a boxlayout, but I get an error saying java.awt.AWTError: BoxLayout can't be shared. I've seen others with this problem, but they solved it by creating the boxlayout on the contentpane, but that is what I'm doing here. Here's my code:
class EditDialog extends JFrame {
JTextField title = new JTextField();
public editDialog() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("New entity");
getContentPane().setLayout(
new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(title);
pack();
setVisible(true);
}
}
Your problem is that you're creating a BoxLayout for a JFrame (this), but setting it as the layout for a JPanel (getContentPane()). Try:
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
);
I've also found this error making this:
JPanel panel = new JPanel(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
The JPanel isn't initialized yet when passing it to the BoxLayout. So split this line like this:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
This will work.
I think that one important thing to highlight from the previous answers is that the BoxLayout's target (the first parameter) should be the same Container that the setLayout method is being called upon as in the following example:
JPanel XXXXXXXXX = new JPanel();
XXXXXXXXX.setLayout(new BoxLayout(XXXXXXXXX, BoxLayout.Y_AXIS));
If you're using the layout on a JFrame like:
JFrame frame = new JFrame();
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
frame.add(new JLabel("Hello World!"));
The control is actually being added to the ContentPane so it will look like it's 'shared' between the JFrame and the ContentPane
Do this instead:
JFrame frame = new JFrame();
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(new JLabel("Hello World!"));

How to organize my JPanel inside a JFrame?

I'm having trouble organizing JPanel inside a JFrame.
I have 3 JPanels and need to organize them as follows:
panel1 panel2
panel__3333333
However, can only horizontally align panel1 and panel2 as follows:
groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
.addComponent(PANEL1)
.addComponent(PANEL2));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup()
.addComponent(PANEL1)
.addComponent(PANEL2));
Thanks.
The easiest way to accomplish this is to house your JPanels inside of other JPanels.
Here is an example of what you might do:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this is the main JPanel that will house all of your child JPanels
JPanel mainPanel= (JPanel)frame.getContentPane();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
//this JPanel will house "panel1" and "panel2" on top and will go inside the main panel
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
//add panel1 and panel2 to topPanel here
mainPanel.add(topPanel);
//add panel3 to mainPanel here
frame.pack();
frame.setVisible(true);
There are many different approaches to this, but I find this the easiest. Hope this helps!
I think you should use GridBagLayout instead of a grouplayout.
It matches with your needs
See the oracle doc on gridbag layout here

how to have JPanel both horizontally and vertically in Java Swing

i am trying to make screen as in the picture using swing.
My code snipet
Label l1,l2,l3;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
l1=new Label("Employee:);
l2=new Label("Earning");
l3=new Label("Deductions");
p1.setLayout(new GridLayout(1,1));
p1.add(l1);
p2.setLayout(new GridLayout(1,1));
p2.add(l2);
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(p1);
container.add(p2);
container.add(p3);
add(container,BorderLayout.NORTH);
setVisible(true);
setSize(600,600);
with Y_AXIS it doesn't allow me to have 2 panel side by side.
how to have panels both horizontally and vertically

Adding labels to the next line of a BoxLayout

I have a box layout that has a JLabel and a button next to it, as such:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
frame.add(panel);
frame.setSize(500,300);
frame.setVisible(true);
This correctly presents a label called reference and a button right next to it. But if I want to present the same thing right below it (a new label, and another button), how would I do that?
Because simply creating another panel, and emulating what I did before, doesn't seem to work.
I.e
JPanel newPanel = new JPanel();
newPanel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
Or using HTML in a new label like
JLabel s = new JLabel("<html> <br>newLaberl </html>");
adding this to the panel still would print it on the same line, after the button, not the next, any ideas?
You just have to set the layout of the JFrame get it working the way you said.
I used your example setting the frame layout to use a GridLayout
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
frame.setLayout(new GridLayout(2,1));
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
panel2.add(new JLabel("Reference2 "));
panel2.add(new JButton("HI2"));
frame.add(panel);
frame.add(panel2);
frame.setSize(500,300);
frame.setVisible(true);
boxlayout isn't maybe the best layout for such thing, i suggest you to go to
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
and take a look on the various layout and tutorial documented on Oracle's docs website.
this one could be usefull for your application
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Control sizing of JPanel

I have a typical IDE style window with a top JMenuBar and JToolBar,
a large center console and a bottom status bar.
Here are the main parts of the code:
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
. (JMenuBar and JToolBar)
.
mainPanel.add(topPanel, BorderLayout.NORTH);
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
.
.
Dimension minDimension = new Dimension(680, 400);
Dimension maxDimension = new Dimension(750, 800);
JPanel centerPanel = new JPanel();
centerPanel.setMinimumSize(minDimension);
centerPanel.setPreferredSize(minDimension);
centerPanel.setMaximumSize(maxDimension);
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(cardPanel);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(statusBar, BorderLayout.SOUTH);
I want to be able to control the sizing of the whole window,
with a minimum and maximum size.
I thought I could have the 'centerPanel' JPanel as a BoxLayout,
so I could have some control over it, but as it is, I can't
control the sizing of the window at all.
I tried to make the 'mainPanel' use a BoxLayout, instead of a BorderLayout, but there
where too many issues.
Is the main BorderLayout causing it to "ignore" the sizing?
I know that the parts, other than the center, have some sizing
control, that's why I tried to use a BoxLayout in the center.
Is it possible to keep the BorderLayout, and get it to work,
with some other modifications, or would I need to switch to some other Layout Manager?
Thanks!

Categories

Resources