Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4610272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:06:28+00:00 2026-05-22T01:06:28+00:00

I have initialized MotePanel, Command Panel and LEDPanel before setting their Layout, then how

  • 0

I have initialized MotePanel, Command Panel and LEDPanel before setting their Layout, then how so am I getting this exception.

Please help.

Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
    at javax.swing.BoxLayout.checkContainer(BoxLayout.java:462)
    at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:246)
    at javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:279)
    at java.awt.Container.addImpl(Container.java:1107)
    at java.awt.Container.add(Container.java:974)
    at javax.swing.JFrame.addImpl(JFrame.java:556)
    at java.awt.Container.add(Container.java:377)
    at Window.<init>(Window.java:54)

public class Window extends JFrame{
    private JPanel MotePanel;
    private JPanel LEDPanel;
    private JPanel CommandPanel;
    private JCheckBox motes[];
    private JRadioButton Leds[];

    public Window(){
        this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
        this.setTitle("Sensor Networks Lab");
        this.setSize(300, 200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        MotePanel = new JPanel();
        LEDPanel = new JPanel();
        CommandPanel = new JPanel();
        motes = new JCheckBox[10];
        Leds = new JRadioButton[3];


        MotePanel.setLayout(new BoxLayout(MotePanel, BoxLayout.Y_AXIS));
        CommandPanel.setLayout(new BoxLayout(CommandPanel, BoxLayout.Y_AXIS));
        LEDPanel.setLayout(new BoxLayout(LEDPanel, BoxLayout.Y_AXIS));

        System.out.println("creating MotePanel");
        for(int i=0; i<10; i++){
            motes[i] = new JCheckBox("Mote "+i);
            MotePanel.add(motes[i]);
        }


        System.out.println("creating LEDPanel");
        for(int i=0; i<3; i++)
            Leds[i] = new JRadioButton();
        Leds[0].setText("RED");
        LEDPanel.add(Leds[0]);
        Leds[1].setText("GREEN");
        LEDPanel.add(Leds[1]);
        Leds[2].setText("BLUE");
        LEDPanel.add(Leds[2]);

        this.add(MotePanel);
        this.add(LEDPanel);
        this.add(CommandPanel);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:06:29+00:00Added an answer on May 22, 2026 at 1:06 am

    When calling setLayout on a JFrame, you’re actually adding the layout to the JFrame’s contentPane not the JFrame itself since this method is more of a convenience method that transmits the method call to the contentPane. The BoxLayout constructor must reflect this since you can’t add the BoxLayout to one container and then pass in as a parameter a different container. So change this:

    this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
    

    to this:

    setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
    

    Also: no need for all the this. business since this. is implied, and also there is no actual need here to extend JFrame.

    edit: this code below is all that is needed to demonstrate your error and its solution:

    import javax.swing.*;
    
    public class BoxLayoutFoo extends JFrame {
       public BoxLayoutFoo() {
    
          // swap the comments below
          setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); // comment out this line
          //setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS)); // uncomment this line
    
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          setVisible(true);
       }
    
       public static void main(String[] args) {
          new BoxLayoutFoo();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with accessing variables initialized in a switch-statement, and then using
I have always initialized my strings to NULL, with the thinking that NULL means
Error % javac StringTest.java StringTest.java:4: variable errorSoon might not have been initialized errorSoon[0] =
$ javac InitInt.java InitInt.java:7: variable right might not have been initialized InitInt(){} ^ 1
I have a UINavigationController containing an UIViewController initialized with a UIView. The UINavigationController also
I currently have 2 BufferedReader s initialized on the same text file. When I'm
I have the following code snippet where some strings are initialized in the if
I have a very big constant array that is initialized at compile time. typedef
I have a piece of code: EDIT: The _penParams are initialized as the added
I have a NSDate *date1 in class1 implementation file(I initialized it as Global variable).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.