-2

Possible Duplicate:
Please help me to sort out this java.awt.BoxLayout can't be shared or NullPointerException

Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:445)
at javax.swing.BoxLayout.invalidLayout(BoxLayout.java:229)
at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:262)
at javax.awt.Container.addImpl(JFrame.java: 1068)
at java.awt.Container.add(Container.java: 935)
at javax.swing.JFrame.addImpl(JFrame.java: 545)
at java.awt.Container.add(Container.java: 352)
at BoxExample.launchFrame(BoxExample.java:26)
at BoxExample.main(BoxExample.java:40)

The code after editing my own previously published post named: "Please help me to sort out this java.awt.BoxLayout can't be shared or NullPointerException"

if you are new to this post also, please look into the code and solve for me:

//Boxlayout

import java.awt.*;
import javax.swing.*;
public class BoxExample
{
public JFrame f;
public JButton b1, b2,b3,b4,b5;

public BoxExample()
{
f=new JFrame("Box example");
f.setTitle("Box Layout Example");
f.setSize(150, 150);
b1=new JButton("Button 1");
b2=new JButton("Button 2");
b3=new JButton("Button 3");
b4=new JButton("Button 4");
b5=new JButton("Button 5");
}

public void launchFrame()
{
System.out.println("inside lf");
f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));
System.out.println("after layset");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.pack();
f.setVisible(true);
}

public static void main(String args[])
{
BoxExample guiWindow=new BoxExample();
System.out.println("main");
guiWindow.launchFrame();
}
}
Community
  • 1
  • 1
  • 1
    You literally asked this 40 minutes ago. Look at [@HovercraftFullOfEel](http://stackoverflow.com/a/11597449/758280)'s answer and provide the code he asked for (there is an edit button at the bottom of your question, so you can amend your first question instead of asking another). – Jeffrey Jul 22 '12 at 03:30
  • oops.. thats ok.. but i heard it too late :) anyway.. thanks!! – DKB - user1543439 Jul 22 '12 at 04:04

1 Answers1

2

BoxLayout cannot be shared, there is a conflict, as the buttons are added to frame's content pane, but the layout is initialized with frame as a target. To fix, replace this:

f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));

with:

f.setLayout(new BoxLayout(f.getContentPane(),BoxLayout.Y_AXIS));
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • though i give the above.. am getting only the same kind of obstacle... anyway dude.. i tried the previous suggestion by you: "public void launchFrame() { System.out.println("inside lf"); JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.Y_AXIS)); System.out.println("after layset"); buttonPane.add(b1); buttonPane.add(b2); buttonPane.add(b3); buttonPane.add(b4); buttonPane.add(b5); f.add(buttonPane); f.pack(); f.setVisible(true); }" which works well.. but may i know wat could be the reason behind it..? – DKB - user1543439 Jul 22 '12 at 04:07
  • huh.. that was my mistake in running the above.. sorry Max the above also did work well..!!! – DKB - user1543439 Jul 22 '12 at 04:10
  • The suggested fix should also work. But you can use separate panel as well. `BoxLayout` makes sure that the layout methods are applied to the correct container. Check [this](http://stackoverflow.com/a/10868110/1048330) for more details. – tenorsax Jul 22 '12 at 04:10