When I initially run my Java Swing application, the padding and margining of the JLabel
and JTextField
components in a JPanel
look good. But whenever I resize the application, the padding and margining for the labels and text fields increases so much that it does not look as good.
I understand a GridLayout
has a set amount of vertical and horizontal spacing, but what can I do to make the spacing look better as the size of the application increases? I would also like the text in the text fields to resize as the text field increases but have been unable to figure it out.
I have been thinking to use a GridBagLayout
but have not had much luck with that either. There is just way too much spacing as the size increases, which does not look good, and I have been unable to find or learn a good solution.
I would appreciate any advice.
Code:
package TestPackage;
import java.awt.GridLayout;
import javax.swing.*;
public class TestGridLayout {
public static void main(String[] args) {
JPanel testPanel = new JPanel();
GridLayout layoutManager = new GridLayout(5,2);
testPanel.setLayout(layoutManager);
JLabel platformEnumerationNameLabel = new JLabel();
JLabel logicalEnumerationNameFieldLabel = new JLabel();
JLabel valueTypeUnitNameFieldLabel = new JLabel();
JLabel measurementNameFieldLabel = new JLabel();
JLabel measurementAxisNameFieldLabel = new JLabel();
JTextField platformEnumerationNameField = new JTextField();
JTextField logicalEnumerationNameField = new JTextField();
JTextField valueTypeUnitNameField = new JTextField();
JTextField measurementNameField = new JTextField();
JTextField measurementAxisNameField = new JTextField();
platformEnumerationNameLabel.setText("Label 1");
testPanel.add(platformEnumerationNameLabel);
platformEnumerationNameField.setText("input some data");
testPanel.add(platformEnumerationNameField);
logicalEnumerationNameFieldLabel.setText("Label 2");
testPanel.add(logicalEnumerationNameFieldLabel);
logicalEnumerationNameField.setText("input some data");
testPanel.add(logicalEnumerationNameField);
valueTypeUnitNameFieldLabel.setText("Label 3");
testPanel.add(valueTypeUnitNameFieldLabel);
valueTypeUnitNameField.setText("input some data");
testPanel.add(valueTypeUnitNameField);
measurementNameFieldLabel.setText("Label 4");
testPanel.add(measurementNameFieldLabel);
measurementNameField.setText("input some data");
testPanel.add(measurementNameField);
measurementAxisNameFieldLabel.setText("Label 5");
testPanel.add(measurementAxisNameFieldLabel);
measurementAxisNameField.setText("input some data");
testPanel.add(measurementAxisNameField);
JDialog dialog = new JDialog();
dialog.add(testPanel);
dialog.setSize(700,200);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}