newbieprogrammer
newbieprogrammer

Reputation: 868

need help on java swing layout

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

Upvotes: 1

Views: 82

Answers (1)

kiheru
kiheru

Reputation: 6618

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.

Upvotes: 3

Related Questions