I'm trying to lay out a basic JPanel using JGoodies.FormLayout
and I'm running into an issue where the JTextField components I place remain at minimum size regardless of anything I do to attempt to resolve the issue. Without further ado,
Program.java
:
package example;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import example.gui.*;
public class Program {
private static JTabbedPane tabbedViewControl;
private static TabPanel someTabPanel;
private static void createAndShowGUI() {
JFrame mainFrame = new JFrame("Stack Overflow Example");
mainFrame.setResizable(true);
mainFrame.setSize(1200, 800);
mainFrame.setTitle("Example");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
tabbedViewControl = new JTabbedPane(JTabbedPane.TOP);
someTabPanel = new TabPanel();
tabbedViewControl.addTab("Example Tab", someTabPanel);
mainFrame.getContentPane().add(tabbedViewControl);
//Display the window.
mainFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
}
example.gui.TabPanel.java
:
package example.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class TabPanel extends JPanel {
private static Border textFieldBorderStyle;
private static ButtonGroup userChoiceButtonGroup;
private static PanelBuilder builder;
private static FormLayout layout;
private static JLabel descriptionLabel;
private static JLabel userChoiceLabel;
private static JLabel nameLabel;
private static JLabel data2Label;
private static JLabel data1Label;
private static JRadioButton userChoice1RadioButton;
private static JRadioButton userChoice3RadioButton;
private static JRadioButton userChoice4RadioButton;
private static JRadioButton userChoice2RadioButton;
private static JRadioButton userChoice5RadioButton;
private static JTextArea descriptionField;
private static JTextField nameField;
private static JTextField data2Field;
private static JTextField data1Field;
private static JPanel subPanel;
public TabPanel() {
super(new GridBagLayout());
initComponents();
}
private void initComponents() {
nameLabel = new JLabel("Name");
nameField = new JTextField();
descriptionLabel = new JLabel("Description");
descriptionField = new JTextArea("");
data1Label = new JLabel("Data No. 1");
data1Field = new JTextField();
data2Label = new JLabel("Data No. 2");
data2Field = new JTextField();
userChoiceLabel = new JLabel("User choices");
userChoiceButtonGroup = new ButtonGroup();
userChoice1RadioButton = new JRadioButton("Choice I");
userChoice3RadioButton = new JRadioButton("Choice II");
userChoice4RadioButton = new JRadioButton("Choice III");
userChoice2RadioButton = new JRadioButton("Choice IV");
userChoice5RadioButton = new JRadioButton("Choice V");
subPanel = constructPanel();
this.add(subPanel);
}
private JPanel constructPanel() {
layout = new FormLayout(
"r:p, 3dlu, l:max(1in;p), 7dlu, r:p, 3dlu, l:p, 3dlu, l:p, 3dlu, l:p",
"p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"
);
builder = new PanelBuilder(layout);
CellConstraints cc = new CellConstraints();
textFieldBorderStyle = nameField.getBorder();
descriptionField.setBorder(textFieldBorderStyle);
userChoiceButtonGroup.add(userChoice1RadioButton);
userChoiceButtonGroup.add(userChoice3RadioButton);
userChoiceButtonGroup.add(userChoice4RadioButton);
userChoiceButtonGroup.add(userChoice2RadioButton);
userChoiceButtonGroup.add(userChoice5RadioButton);
builder.addSeparator("Name & Description", cc.xyw(1, 1, 3));
builder.add(nameLabel, cc.xy (1, 3));
builder.add(nameField, cc.xy (3, 3));
builder.add(descriptionLabel, cc.xy (1, 5));
builder.add(descriptionField, cc.xywh(3, 5, 1, 4));
builder.addSeparator("Other Data for the User to Enter", cc.xyw(5, 1, 5));
builder.add(data1Label, cc.xy(5, 3));
builder.add(data1Field, cc.xy(7, 3));
builder.add(data2Label, cc.xy(5, 5));
builder.add(data2Field, cc.xy(7, 5));
builder.add(userChoiceLabel, cc.xy(5, 7));
builder.add(userChoice2RadioButton, cc.xy(7, 7));
builder.add(userChoice5RadioButton, cc.xy(7, 9));
builder.add(userChoice3RadioButton, cc.xy(7, 11));
builder.add(userChoice4RadioButton, cc.xy(9, 7));
builder.add(userChoice1RadioButton, cc.xy(9, 9));
return builder.getPanel();
}
}
The following is the result:
Ideally, I'd like for three things to occur, rather than above:
- The JPanel returned from
PanelBuilder
to occupy the full dimensions of its parent - The JTextFields and JTextArea to be of reasonable width (and not resize*)
- The two aspects to be more proportional - I'm not expecting 50%/50%, but I don't want the right side to squash the left side up against the border
I've done a fair amount of homework on this, but mostly came up with the following non-results:
- Attempting to search on SO mainly yields issues with "native" AWT layout systems
- Going through the JGoodies Forms whitepaper, while giving me the use of
max(constantSize;preferredSize)
, only assisted in the resizing of the separator, and did nothing re: the JTextFields
So, in short, is there a way to force JTextFields to occupy the given width of their column in a FormLayout
?