File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Changing a Layout in JPanel at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Changing a Layout in JPanel at runtime" Watch "Changing a Layout in JPanel at runtime" New topic
Author

Changing a Layout in JPanel at runtime

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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

Joined: Oct 27, 2005
Posts: 19216

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?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

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
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

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

Joined: Aug 08, 2010
Posts: 210

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Changing a Layout in JPanel at runtime
 
Similar Threads
JPanel not repainting properly.
Layered JPanels Transparency Problem.
GridBagLayout Problem
Custom painting/graphics
Adjust font size according to resolution change (or different screen size)