• Post Reply Bookmark Topic Watch Topic
  • New Topic

BoxLayout cannot be shared

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to use a BoxLayout to make the equivalent of the vertical flow layout within a JPanel. I followed this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html, but was a little confused as to what the first argument in the constructor for the BoxLayout is (the tutorials that it was "the container that it manages," but I don't know what that means). All the examples I can find online just have the first argument as what ever kind of component they are trying to initialize using the BoxLayout, so I tried that but it produces the error "BoxLayout cannot be shared. " Anyone know how to fix this? Here is my code:

//Main class


//GUI class
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried to get it working? Because the solution is very easy.

Greetz,
Piet
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure why the exception would be "BoxLayout can't be shared" but I do know how to fix this and possibly why it's giving you this message.

Let's look at a snippet from your code,



I think the issue is that when you're passing an object of type LayoutManager, in this case BoxLayout, to the JPanel constructor, an argument for the BoxLayout is the object you are currently instantiating. To clarify, look at what is being passed through the JPanel constructor. You are passing a new BoxLayout object that has two arguments being passed through to its own constructor. So, essentially what it seems like you're doing is passing a JPanel as a container through to the BoxLayout constructor, but you haven't finished instantiating the JPanel yet. In the end, you're really passing a null reference through as your container object.

This would almost look like...


Looks pretty goofy, eh?

To fix this, just instantiate that mainPanel object and use setLayout() to set the layout manager.

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure the problem lies in the first argument of the constructor of the BoxLayout, so I have tried replacing that argument with other stuff, including getContentPane(), and most recently, at Skye's behest, null. However, I still get the error "Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared" in the line in the Main class where I actually create the GUI object.

Perhaps you could help me out, Piet, if the solution is easy as you say?
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:Perhaps you could help me out, Piet, if the solution is easy as you say?


You mean you would like Piet to repeat this?
Skye Antinozzi wrote:To fix this, just instantiate that mainPanel object and use setLayout() to set the layout manager.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apologies to Skye, I didn't really read your post at first -- I just looked at the snippet of code you posted. After actually reading it,I changed the offending bit of code to the following:



thanks!
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Louis Lewis wrote:Perhaps you could help me out, Piet, if the solution is easy as you say?


You mean you would like Piet to repeat this?
Skye Antinozzi wrote:To fix this, just instantiate that mainPanel object and use setLayout() to set the layout manager.


yeah my bad, just read that.
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are generous Ranchers... Be sure to read carefully next time you are being assisted. ^_^
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:apologies to Skye, I didn't really read your post at first -- I just looked at the snippet of code you posted. After actually reading it,I changed the offending bit of code to the following:



thanks!

hi Louis,

it seemed almost impossible to me that you had not tried this out, and so would not
have had this issue. That's why I was interested to see what you did try.
But I'm glad the issiue is gone now. And while you're on the BoxLayout,
don't forget to have a look at the glue's and the fillers.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!