Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BoxLayout: Sharing ISN'T Caring... :(

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Question BoxLayout: Sharing ISN'T Caring... :(

    I'm trying to lay out some radio buttons using BoxLayout, which aligns Components right next to each other vertically or horizontally. In other words, it makes them nice and neat. However, I'm running into the exception:
    "BoxLayout can't be shared"

    I have been aware of this exception for a long time, but I didn't expect it to arise in the situation it did:
          //... other stuff ...
     
          gameModePanelLabel = new JLabel("Choose the type of game.", SwingConstants.CENTER);
          compVcompModeOption = new JRadioButton("Computer vs. Computer");
          playVcompModeOption = new JRadioButton("Player vs. Computer");
          playVplayModeOption = new JRadioButton("Player vs. Player");
          gameModes = new ButtonGroup();
          gameModes.add(compVcompModeOption);
          gameModes.add(playVcompModeOption);
          gameModes.add(playVplayModeOption);
          gameModePanel = new JPanel(new BoxLayout(gameModePanel, BoxLayout.PAGE_AXIS));
          gameModePanel.setBorder(BorderFactory.createTitledBorder("Game Mode"));
          gameModePanel.add(gameModePanelLabel);   //There exception occurs in this line, but I assume
          gameModePanel.add(compVcompModeOption);  //that the three lines below it will also provoke
          gameModePanel.add(playVcompModeOption);  //an exception
          gameModePanel.add(playVplayModeOption);
     
          //... and yet other stuff ..

    I have no idea why I am getting this error, as I am only adding Components to a JPanel with a BoxLayout...

    Will someone please point out my silly mistake?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: BoxLayout: Sharing ISN'T Caring... :(

    Weeeell, never mind. I got it to work, simply by changing...
    gameModePanel = new JPanel(new BoxLayout(gameModePanel, BoxLayout.PAGE_AXIS));
    ...to...
    gameModePanel = new JPanel();
    gameModePanel.setLayout(new BoxLayout(gameModePanel, BoxLayout.PAGE_AXIS));

    Isn't that weird...

    Would someone be able to offer an explanation as to why that worked?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: BoxLayout: Sharing ISN'T Caring... :(

    Your confusion is probably due to the way Java manages object references.

    Initially, gameModePanel points to some panel (or more likely, null).

    In the first example, you pass that old panel (or null) to the BoxLayout constructor, so it's managing the layout of another component. Then when you try to use it, it freaks out because you don't have access rights to the other component.

    In the second example, gameModePanel is initialized to the correct container first. Then when you try to set the layout to a BoxLayout, you pass in the already initialized gameModePanel, so that the BoxLayout manages the correct gameModePanel and everything is happy.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    snowguy13 (December 23rd, 2011)

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: BoxLayout: Sharing ISN'T Caring... :(

    @helloworld922

    Okay, that explains a lot. Thank you!

    Hmm... But that brings another question to my mind: why does this problem only occur with BoxLayout, and not GridLayout, GridBagLayout, or other such LayoutManagers?
    Last edited by snowguy13; December 23rd, 2011 at 07:36 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: BoxLayout: Sharing ISN'T Caring... :(

    GridLayout doesn't take the component as a parameter so this situation can't happen. Same for GridBagLayout. I'm think other Layout Managers are the same way (these are the two main layouts I use).

    I am slightly curious as to why BoxLayout even requires the component in the constructor, but I haven't really ever used BoxLayout before.

  7. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: BoxLayout: Sharing ISN'T Caring... :(

    I am slightly curious as to why BoxLayout even requires the component in the constructor
    I share that curiosity. When I first used BoxLayout, I was thrown off by having that parameter in its constructor. It seems redundant.

    But perhaps its as you said before. Instead of the component controlling the BoxLayout, perhaps BoxLayout needs to be independent and controls the component from a somewhat third-person-ish perspective...

    Anyways, thanks for you help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Extending a Servlet and sharing HttpServletRequest
    By mastervh in forum Java Servlet
    Replies: 6
    Last Post: October 12th, 2011, 03:51 AM
  2. By using java how to listout sharing files in LAN...?
    By ram07 in forum Java Networking
    Replies: 4
    Last Post: March 26th, 2011, 10:18 PM
  3. Sharing my ByteStream, feel free to improve!
    By Verficon in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2011, 10:44 AM

Tags for this Thread