• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Which LayoutManager To Use ?

 
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 ...
 
Marshal
Posts: 65104
247
  • 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 ]
 
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
Marshal
Posts: 65104
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or maybe BoxLayout???
 
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
Boost this thread!