Re: JButton sizing ?



Tony B wrote:
Having got my cardlayout working (see earlier post), I am now trying to understand layout of components better.

I created a JPanel with a JTextfield across the top of the panel, full width, and one JButton below on the right. I'm using a borderlayout, with the textfield set PAGE_START, and button set LINE_END.
The basic arrangement is as expected, with the textfield across the top, and the button below on the right. My problem is the button vertically fills the whole pane from below the textfield to the bottom of the pane.
What I want to is fix the height of the button so it's fixed. and doesn't grow as the pane is resized.
I've tried using
button.setMaximumSize(new Dimension (150,10)) ;
button.setMinimumSize(new Dimension (150,10)) ;
button.setPreferredSize(new Dimension (150,10)) ; to try to force the size but these are ignored.

Is this possible ?


import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestBorder extends JPanel {

TestBorder() {
setLayout(new BorderLayout());
add(new JTextField(), BorderLayout.PAGE_START);
//add(new JButton("OK"), BorderLayout.LINE_END);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(Box.createVerticalGlue());
p.add(new JButton("OK"));
add(p, BorderLayout.LINE_END);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Test BorderLayout");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TestBorder());
f.pack();
f.setVisible(true);
}
});
}

}
.


Quantcast