Reputation: 1099
Please see below picture showing small part of a GUI I’m working on. Also below is the code that generated it. This GUI is almost exactly what I want except for one thing. I want the JTextField
in the second row to only be 1/3 as wide as it is and anchored on left hand side. Thus it will be directly under and the exact same width as the first JTextfield
above it. And, it must maintain this position and size relationship as the frame is re-sized in the horizontal direction.
So, how do I add invisible fill that will always ocupy the right 2/3 of the row as the horizontal size is changed? At this time, I’m not concerned about the verticle re-sizing behavior.
Thanks.
import java.awt.*;
import javax.swing.*;
public class BoxTest {
private static void createAndShowGui() {
Dimension comboBoxSize = new Dimension(70,30);
JComboBox comboBox1 = new JComboBox(new String[]{"A", "B", "C"});
JComboBox comboBox2 = new JComboBox(new String[]{"D", "E", "F"});
JComboBox comboBox3 = new JComboBox(new String[]{"G", "H", "I"});
JComboBox comboBox4 = new JComboBox(new String[]{"J", "K", "L"});
comboBox1.setPreferredSize(comboBoxSize);
comboBox2.setPreferredSize(comboBoxSize);
comboBox3.setPreferredSize(comboBoxSize);
comboBox4.setPreferredSize(comboBoxSize);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(650, 400);
JPanel boxPanel = new JPanel();
BoxLayout bl0 = new BoxLayout(boxPanel, BoxLayout.Y_AXIS);
boxPanel.setLayout(bl0);
JPanel row1Panel = new JPanel(new BorderLayout());
JPanel hBox1 = new JPanel();
BoxLayout bl1 = new BoxLayout(hBox1, BoxLayout.X_AXIS);
hBox1.setLayout(bl1);
JTextField t1 = new JTextField(15);
JTextField t2 = new JTextField(15);
JTextField t3 = new JTextField(15);
hBox1.add(t1);
hBox1.add(t2);
hBox1.add(t3);
row1Panel.add(comboBox1, BorderLayout.WEST);
row1Panel.add(comboBox2, BorderLayout.EAST);
row1Panel.add(hBox1, BorderLayout.CENTER);
boxPanel.add(row1Panel);
JPanel row2Panel = new JPanel(new BorderLayout());
JPanel hBox2 = new JPanel();
BoxLayout bl2 = new BoxLayout(hBox2, BoxLayout.X_AXIS);
hBox2.setLayout(bl2);
JTextField t4 = new JTextField(15);
hBox2.add(t4);
row2Panel.add(comboBox3, BorderLayout.WEST);
row2Panel.add(comboBox4, BorderLayout.EAST);
row2Panel.add(hBox2, BorderLayout.CENTER);
boxPanel.add(row2Panel);
frame.add(boxPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 0
Views: 286
Reputation: 324167
I might try something like:
Upvotes: 3
Reputation: 64
You could use a GridBagLayout for your hBox2 to better adjust with GridBagConstraints and use constraints to force the 1/3rd feel. Last that I can think of is that you could revamp the overall layout and try a GroupLayoutManager. A Visual Guide to Layout Managers is a good source for tutorials with Swing layout managers. Also, forcing a preferred size on a component is asking for the layout manager to be finicky.
Upvotes: 0