• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing a Layout in JPanel at runtime  RSS feed

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a frame with two panels, one is with JRadio buttons to choose a layout for the second panel,that sits above it.I can't get the panel to change layouts?

I have an Itemlistener and check wich layout is set, but not sure how to continue.I am not sure where then to call the setLayout(layout); layout is a LayoutManager variable. I have been searching for some sample code for this on the net but nothing really with what i am trying to do. I thought this would be a simple thing to do, but at least at this point it isn't for me.

Thanks
Mike
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing a layout at runtime isn't easy. Just calling setLayout isn't enough; the layout won't have any constraints for all the controls that are already present. BorderLayout for instance stores for itself what the center, north, etc components are, but only for components that are added after the layout has been set.

What layouts are we talking about?
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

well i was going to use GridLayout, but the layout itself isn't all that important, and it doesn't have tpo be a complex set-up. I really just want to see how this is done in general.
I am currently just setting and instance variable layout, to layout=(new GridLayout(3,1)); to try in the ItemListener in a listener class. But then i get stuck in not knowing where i need to call the setLayout() and validate().
Do i need to create an extra method to do it? Or can i set a new layout in the JPanel itself, and how to then change the current default layout to the new one?

just the idea of doing this seems like it should be simple but i guess not as simple as i thought!

Mike
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:I really just want to see how this is done in general.


In general, it isn't done. The only use case I can imagine for that feature is if you were writing a GUI designer.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
mike ryan wrote:I really just want to see how this is done in general.


In general, it isn't done. The only use case I can imagine for that feature is if you were writing a GUI designer.


Well it is possible that some day if i ever learn Java well enough i will write a GUI Designer ;) although at this time i have serious doubts that i will ever get the hang of Java
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!