1

How can I make a gui layout in java which is column based? by that i mean:

JLabel
JTextField
JLabel
JTextField
JLabel
JTextField

I want them all stacked on to of each other, rather than side by side. In the past I've used FlowLayout which isn't suitable, and gridLayout which makes each component much larger than I require. Any ideas?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user650309
  • 2,639
  • 7
  • 28
  • 47
  • 1
    [Laying Out Components Within a Container](http://download.oracle.com/javase/tutorial/uiswing/layout/index.html) – mre Jun 09 '11 at 20:46

5 Answers5

6

Look at BoxLayout, but more important, go through the layout manager tutorial to get an overview of all the user-friendly layout managers.

Edit 1:
Also, GridLayout sometimes works well in this situation, but you may want to place your JTextFields inside of JPanels and add the JPanels to the grid, so that the JTextFields aren't huge looking.

Edit 2:
If you have a bunch of JTextFields with associated JLabels, I've had success and fun working from an array of String that represents the JLabel texts, and then placing the JTextFields into a Map<String, JTextField> so that I can easily get a reference to the JTextField based on it's related String. Example to follow...

Edit 3
as promised, the example:

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
public class LabelsAndFields {
   public static final String[] LABEL_TEXTS = {
      "Sunday", "Monday", "Tuesday",
      "Wednesday", "Thursday", "Friday", "Saturday"};

   private JPanel mainPanel = new JPanel();
   private static final int FIELD_COLS = 10;
   private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();

   public LabelsAndFields() {
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
      for (String labelText : LABEL_TEXTS) {
         JTextField textField = new JTextField(FIELD_COLS);
         textFieldMap.put(labelText, textField);
         JPanel panel = new JPanel(new BorderLayout());
         panel.add(new JLabel(labelText), BorderLayout.NORTH);
         panel.add(textField);
         int gap = 8;
         panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
         mainPanel.add(panel);
      }
   }

   public String getText(String labelTextKey) {
      JTextField field = textFieldMap.get(labelTextKey);
      if (field == null) {
         throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
      }

      return field.getText();
   }

   public void setText(String labelTextKey, String text) {
      JTextField field = textFieldMap.get(labelTextKey);
      if (field == null) {
         throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
      }

      field.setText(text);
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private static void createAndShowUI() {
      final LabelsAndFields labelsAndFields = new LabelsAndFields();

      JButton showAllTextBtn = new JButton(new AbstractAction("Show All Text") {
         @Override
         public void actionPerformed(ActionEvent arg0) {
            for (String labelText : LabelsAndFields.LABEL_TEXTS) {
               System.out.printf("%10s: %s%n", labelText, labelsAndFields.getText(labelText));
            }
         }
      });

      JFrame frame = new JFrame("LabelsAndFields");
      frame.getContentPane().add(labelsAndFields.getMainPanel(), BorderLayout.CENTER);
      frame.getContentPane().add(showAllTextBtn, BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

you could use a BoxLayout and in the constructor set the layout type to Box.Y_AXIS, and with this all of the widgets will fall under the previous one added, OR you could still use the GridLayout but add each individual widget to its own JPanel before adding that JPanel to the GridLayout, that will prevent the oversizing problem that you would normally have, and btw you dont have to store any reference to the JPanel's just instantiate, add a widgets, and add the panel until all widgets are taken care of

1

BoxLayout is what you need:

getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
1

I'd suggest MigLayout, it's very simple and very powerful.

In this case all you need to do is

JPanel p = new JPanel(new MigLayout("wrap")); 
p.add(new JLabel());
p.add(new JTextField());
p.add(new JLabel());

etc..

meverett
  • 921
  • 5
  • 6
  • 1+ yep, a very useful and versatile layout manager. The OP would need to download it and place it on the classpath to use it. The link can be found here: [MiG Layout - *The* Java Layout Manager](http://www.miglayout.com/) – Hovercraft Full Of Eels Jun 09 '11 at 22:33
0

I would suggest gridlayout. You can choose he rows and columnsime a table

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53