1

Below are my code to add label and textfield. i want to add it such that its [label] [textfield] in 2 column

i know i can do it by using gridlayout but the number of rows need to be fixed, hence is there another method to add it such that no matters how many [label] [textfield] i add in, it will be 2 column

the picture shows the expected view

but currently it is [label] [textfield] [label2] [textfield2] [label3] [textfield3]

  public WizardPage page1() {
           WizardPage page1 = new WizardPage("1", "Page 1") {
               {
                   JTextField txt1 = new JTextField();
                   JTextField txt2 = new JTextField();
                   JTextField txt3 = new JTextField();
                   JTextField txt4 = new JTextField();

                   txt1.setName("text1");
                   txt2.setName("text2");
                   txt3.setName("text3");
                   txt4.setName("text4");

                   txt1.setPreferredSize(new Dimension(50, 20));
                   txt2.setPreferredSize(new Dimension(50, 20));
                   txt3.setPreferredSize(new Dimension(50, 20));
                   txt4.setPreferredSize(new Dimension(50, 20));

                   add(new JLabel("text1"));
                   add(txt1);
                   add(new JLabel("text2"));
                   add(txt2);
                   add(new JLabel("text3"));
                   add(txt3);
                   add(new JLabel("text4"));
                   add(txt4);
               }
           };
           return page1;
       }



>    public WizardPage(String title, String description){
>        
>        PropertyConfigurator.configure("config/log4j.properties");
>        log = Logger.getLogger(WizardPage.class);
>       
>       _title = title;
>       _description = description;
>       
> 
>     setLayout(new FlowLayout()); );


      addContainerListener(new WPContainerListener());
      this.setDoubleBuffered(true);
   }

enter image description here

newbieprogrammer
  • 848
  • 7
  • 23
  • 46
  • http://stackoverflow.com/questions/12835198/positioning-of-components-how-to-place-a-few-buttons-center-screen-same-size/12837118#12837118 Use BoxLayout, check my other answers to find more examples – Gianmarco Sep 20 '13 at 10:28
  • but doesnt boxlayout make everything in 1 column? – newbieprogrammer Sep 20 '13 at 10:30
  • 1
    You can nest layouts, so `BoxLayout` can be used. If you want to use `GridLayout`, use the constructor as: `new GridLayout(0, 2)`. That sets the number of columns to 2, and rows are added as needed. – kiheru Sep 20 '13 at 10:36
  • but if i have 4 row i have to change to GridLayout(4, 2). cant it be more dynamic such that whatever number of rows it will always be 2 column – newbieprogrammer Sep 20 '13 at 10:43
  • 2
    Please read what I wrote: *That sets the number of columns to 2, and rows are added as needed*. Setting rows to 0 gives the dynamic behaviour you want. – kiheru Sep 20 '13 at 10:52
  • @kiheru - you should put your comments in an answer. – Qwerky Sep 20 '13 at 10:59
  • @newbieprogrammer How to use GridLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html – Qwerky Sep 20 '13 at 10:59
  • @newbieprogrammer absolutely not, boxlayout don't make everything in 1 column, with the correct nesting you can make almost everything you want, please read my link and you will be cleared on the argument – Gianmarco Sep 20 '13 at 12:53

1 Answers1

3

The simplest way to have unspecified number of rows in a grid is to use GridLayout with 0 as the row count:

setLayout(new GridLayout(0, 2));

This will result in 2 columns, and rows being added as needed. The downside of GridLayout is that the labels and text fields will have the same width, which may not be what you want.

Another easy approach is nesting layouts: you could place each label + text field combination to a JPanel, and then continue adding those panels to a vertical BoxLayout as needed. This neither, is flawless, and can result in a layout like:

label | text
-----------------
long label | text

If you need a grid that can make the columns and rows different width, investigate other grid based layouts: GridBagLayout comes with the standard library, and is capable of such grids, but is a bit complicated to use. If you find yourself making a lot of such layouts, consider using a third party layout manager, such as MiGLayout.

kiheru
  • 6,588
  • 25
  • 31