• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which LayoutManager To Use ?

 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell and Tapan for the great guide, i will do what you guys suggested, thanks alot ...

My Best Regards ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or maybe BoxLayout???
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic