Faced with the problem of using BoxLayout
In my example, I try to decrease the height of the text field and change the width of the buttons (as shown in green marker in the picture). I know about the techniques setPrefferedSize () and setMaximumSize (), but it did not work as it should. The line add(Box.createHorizontalGlue ()) also did not help.
Thanks for any idea
public class Testy extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
constructGUI();
}
});
}
private static void constructGUI() {
JFrame frame = new JFrame("Testy");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.DARK_GRAY);
centerPanel.setPreferredSize(new Dimension(100, 400));
frame.add(centerPanel, BorderLayout.CENTER);
Testy eastPanel = new Testy();
frame.add(eastPanel, BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public Testy() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JButton button = new JButton("Button ...... 1");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button 2");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
button = new JButton("Button ........... 3");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
JLabel label = new JLabel("Label");
//label.setPreferredSize(...);
//label.setMaximumSize(...);
add(label);
JTextField textField = new JTextField();
//textField.setPreferredSize(...);
//textField.setMaximumSize(...);
add(textField);
button = new JButton("Button 4");
//button.setPreferredSize(...);
//button.setMaximumSize(...);
add(button);
//add(Box.createHorizontalGlue());
}
}
This should get close, based on your draw, just need to work on that component below the JLabel (using setPreferredSize()):
First you have to realize that component position and size in Java Swing depends on Layout manager (if layout manager is set) not on the component itself. The component requests the manager for size.
For this case I would use different layout - combination of GridLayout and BorderLayout is enough and very simple and straightforward. But if want use BoxLayout, then...
Documentation says:
Then set components maximum size:
c.setMaximumSize(new Dimension(Integer.MAX_VALUE, c.getMinimumSize().height));
(c
meansbutton
,label
andtextField
in your example)Edit 1:
Here is working source code:
Edit 2:
If you want laid out Button 4 at the bottom of right column add this line
add(Box.createVerticalGlue());
betweenadd(textField);
andbutton = new JButton("Button 4");
.As a quick remedy, you can use nested layouts, in the sense, that on the right side, create a
JPanel
withBorderLayout
, put aJPanel(say compPanel)
at theCENTER
and aJPanel(say buttonPanel)
atPAGE_END
location. Now use a newJPanel(say panel)
withGridLayout
and put all the components on it, and place thiscompPanel
insidecenterPanel
. PlaceJButton(button4)
insidebuttonPanel
as is.BoxLayout
on the contrary, respects the preferred size of a givenJComponent
, which is usually calculated based on the content theJComponent
holds or given explicity, hence components do not tend to align well with respect to other given components.Here is the working example :
OUTPUT :