3

Problem Steps

Hi,

I have a requirement where the UI has to be dynamically generated. Please see the above image. Step 1 consists of a Dialog with a jcombobox and and jbutton. When I click the "+" button in step 1, a new row should get dynamically added to the jdialog, this is shown in Step 2. Similarly, more rows can be added using the "+" button. Pressing the "-" button should remove the row. Also, the JDialog should have a vertical scroll bar when necessary. I have been trying Grid layout but to no avail. Any help will be appreciated.

Will
  • 482
  • 1
  • 8
  • 21

3 Answers3

1

I guess that your UI should use a JScrollPane to be able to scroll when necessary... I can't understand why GridLayout is not avilable , what is your error ? Did you do the correct import in your code ? Could you give us more details

Jerome

romje
  • 650
  • 3
  • 4
  • Guesses and questions should be saved for comments. – Andrew Thompson Jan 10 '12 at 10:47
  • It is available, but the new components being added were getting squeezed into the panel area and the panel size was not increasing. The validate and revalidate methods did the trick. – Will Jan 10 '12 at 10:57
1

Try this link on how to add components on runtime and this thread has an example to add/ remove jbuttons on runtime

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • Ok. I was adding the components but not calling the validate and revalidate methods on the dialog and frame respectively. The first link did the trick. Thanks O.D – Will Jan 10 '12 at 10:55
1

You will need a JScrollPane from the start. Luckily for you JScrollPane allows to automatically show/hide the scrollbars when they are (not) needed. See the JScrollPane#set*ScrollBarPolicy.

For your panel you will have to use a layout which allows to add components dynamically, like for example a FlowLayout. A non-core Java layout which can also be used for this case is the JGoodies FormLayout with a dynamic row builder. This FormLayout will easily allow to keep those columns used in your screenshot while allowing to dynamically add rows at runtime.

Robin
  • 36,233
  • 5
  • 47
  • 99