aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Which LayoutManager To Use ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Which LayoutManager To Use ?" Watch "Which LayoutManager To Use ?" New topic
Author

Which LayoutManager To Use ?

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

I'm developing a JFrame that will add some components dynamically.
When i add a new component, i want all components that are below, to be shifted down, and when i remove it, i want everything to return as it was.

Is there a specific LayoutManager to use in order to fulfill my requirements ?

Thanks in advance ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That sounds difficult . . .

This is just a suggestion it might work and might not . . .

Try GridBagConstraints:-
Are you familiar with Cay Horstmann's GBC class? It extends GridBagConstraints and makes GridBagLayout easier to use. Go through it until you understand it. There is a description of the GBC class in Horstmann's book pages 430-434 (C Horstmann, G Cornell, Core Java 2-vol I Fundamentals 7/e, Santa Clara: Sun Microsystems Press (Prentice-Hall) (2004)).

Try putting all your Components into a List; add to and remove from the position 0 in the List.
Whenever you add a Component, remove everything, then replace everything by going through the List; use the index value to set the gridy value of the Constraints, then call validate().
Whenever you remove a Component, you will have to remove it from the List and the JFrame, then again go through the indices to set up the gridy value.

Alternative suggestion:-
You may be able to use a GridLayout with one column instead and always add or remove at index 0.
[ March 28, 2007: Message edited by: Campbell Ritchie ]
Tapan Maru
Ranch Hand

Joined: May 08, 2006
Posts: 65
You can use GridLayout which has a constructor which takes the rows and columns.

you can give column as 1 there and rows as 0. that will probably solve your problem

getContentPane().setLayout(new GridLayout(0,1));


try out and give feedback whether it works or not


Tapan Maru
tapanmaru@gmail.com
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Thanks Campbell and Tapan for the great guide, i will do what you guys suggested, thanks alot ...

My Best Regards ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
. . . or maybe BoxLayout???
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Often it's simpler to write your own layout manager than to figure out how to get your requirements implemented in other layout managers.

Implementing a layout manager is actually quite easy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which LayoutManager To Use ?