0

I have a GUI program which includes JLabels and JButtons and basically I want a layout that would help me display them as follows:

Label1     Button1
Label2     Button2
Label3     Button3
.....
 

Is there a layout that would allow me to achieve the above result?

I have looked at this example but is too complex and was wondering if there is anything automated that I can use?

Community
  • 1
  • 1
user3353723
  • 221
  • 1
  • 7
  • 15
  • Maybe you should make a Box: [link](http://docs.oracle.com/javase/7/docs/api/javax/swing/Box.html) for both of them with Axis layout and then just add them to the panel (BorderLayout.EAST and BorderLayout.WEST), you can find more about BorderLayout: [link](http://download.java.net/jdk7/archive/b123/docs/api/java/awt/BorderLayout.html). – AdamK May 25 '14 at 06:28
  • `Label1 Button1` BTW - given a button can have text (an icon) and a tool-tip, what is the label for? Does it actually relate to the button, if so, how? – Andrew Thompson May 25 '14 at 06:40

4 Answers4

3

This is one of the few things for which I'd recommend (a utility method and) GroupLayout as seen in this answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

You can use GridLayout. Documentation here.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

This is just for simplicity, and for your question. GUI is really dependent on what you would like to do and is really a thing that can be hardly automated..., and i don't think you only want those 6 elements on your GUI, but theoretically this will do it:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GUITest {

    private Box labelbox = new Box(BoxLayout.Y_AXIS); 
    //Y_AXIS means they are placed vertically in the box
    private Box buttonbox = new Box(BoxLayout.Y_AXIS);
    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();

    public void makeGUI1() {
        for (int i = 1; i <= 3; i++) { 
            //if you want to save the references, you should make 
            //an ArrayList<JLabel> and add each of them to it
            JLabel label = new JLabel("Label " + i);
            labelbox.add(Box.createVerticalStrut(5)); 
            //these are for giving the labels some extra space 
            //between them vertically to be in line with the buttons
            labelbox.add(label);
            labelbox.add(Box.createVerticalStrut(10)); //these are too

        }
        for (int i = 1; i <= 3; i++) { 
            //if you want to save the references, you should make 
            //an ArrayList<JButton> and add each of them to it
            JButton button = new JButton("Button " + i);
            buttonbox.add(button);
        }
        panel.add(labelbox, BorderLayout.EAST); 
        //you can find picture of each constant: 
        //http://download.java.net/jdk7/archive/b123/docs/api/java/awt/BorderLayout.html
        panel.add(buttonbox, BorderLayout.WEST);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUITest guitest = new GUITest();
                guitest.makeGUI1();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
AdamK
  • 177
  • 10
-1

You can also use obj.setBounds(LeftSpaceParameter,TopSpaceParameter) with which you can place the gui elements or objects at any position of your choice. You need to put the default layout to null yet gridLayout is much easier. .

  • Don't suggest `null` layouts when Java SE provides a slew of better ways (i.e. layouts, or combinations of them) to arrange the components. – Andrew Thompson May 25 '14 at 06:36