3

Is there anyway of getting JPanel resized (vertically) automatically as the components are added? I have assigned the layout manager to null.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 7
    *"Is there anyway of getting JPanel resized (vertically) automatically as the components are added?"* Sure, the first step is.. *"I have assigned the layout manager to null."* ..use an appropriate layout manager. – Andrew Thompson Sep 11 '12 at 23:37
  • How are you designing your JPanel? Are you using the straight Swing code and a command-line compiler or are you using a GUI Builder in NetBeans or Eclipse? – Code-Apprentice Sep 11 '12 at 23:37
  • I am using Netbeans. But, some components are to be added dynamically depending on other values. – FirmView Sep 11 '12 at 23:40
  • 2
    See the [nested layout example](http://stackoverflow.com/a/5630271/418556) - the labels 1 through 25 are dynamically added. – Andrew Thompson Sep 11 '12 at 23:40
  • *"depending on other values."* Can you vague that up for me? What is the logic behind the positioning? Is it a seating plan? A schematic? A family tree? What..? – Andrew Thompson Sep 11 '12 at 23:42
  • 5
    I think that you are hampering your program's abilities by using null layout. As many of the experienced Swing programers are telling you -- learn about and use the layout managers. Then this problem often becomes trivial. – Hovercraft Full Of Eels Sep 12 '12 at 00:00

1 Answers1

5

Concider using the fitting layout manager that will help you expand the way you want to when you add the components

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

I think you are looking for the GridLayout and possibly use FlowLayout JPanels inside so that they dont expand, but this will depend on what you want to insert.

nick
  • 86
  • 4