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 ?
Thomas Fritsch - 28 Feb 2007 16:55 GMT
> Having got my cardlayout working (see earlier post), I am now trying to
> understand layout of components better.
[quoted text clipped - 14 lines]
>
> Is this possible ?
Yes, but not with BorderLayout alone. The trick is to use nested JPanels,
each with its own different layout manager.
A good tutorial describing this technic is
"Effective Layout Management: Short Course"
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
With proper use of layout managers you should never need to call
setMaximumSize/setMinimumSize/setPreferredSize

Signature
Thomas
TonyB - 28 Feb 2007 23:51 GMT
>> Having got my cardlayout working (see earlier post), I am now trying to
>> understand layout of components better.
[quoted text clipped - 22 lines]
> With proper use of layout managers you should never need to call
> setMaximumSize/setMinimumSize/setPreferredSize
Thanks Thomas. That article is good at explaining this topic.
Tony
Ian Wilson - 28 Feb 2007 17:27 GMT
> Having got my cardlayout working (see earlier post), I am now trying to
> understand layout of components better.
[quoted text clipped - 14 lines]
>
> 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);
}
});
}
}
TonyB - 28 Feb 2007 23:50 GMT
>> Having got my cardlayout working (see earlier post), I am now trying to
>> understand layout of components better.
[quoted text clipped - 51 lines]
>
> }
Thanks That works a treat. This seem a bit like writing hml using nested
tables to get a design to look right !
Tony
Ian Wilson - 01 Mar 2007 09:20 GMT
>>> 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
[quoted text clipped - 22 lines]
> Thanks That works a treat. This seem a bit like writing hml using nested
> tables to get a design to look right !
It is, except that HTML tables were originally intended for tabular data
not for layout, whereas JPanels and Layouts were always intended to be
used for layout and to be nestable. Perhaps it is more like nested DIVs
with CSS :-)
If you don't like nesting layouts in this way, you can achieve the same
visual effect by using (the notoriously difficult) GridBagLayout, (the
popular) JGoodies FormLayout or (my favourite) MigLayout without the
need for nesting of layouts and panels.
Tony B - 01 Mar 2007 17:11 GMT
----- Original Message -----
From: "Ian Wilson" <scobloke2@infotop.co.uk>
Newsgroups: comp.lang.java.gui
Sent: Thursday, March 01, 2007 9:20 AM
Subject: Re: JButton sizing ?
>> Thanks That works a treat. This seem a bit like writing hml using nested
>> tables to get a design to look right !
[quoted text clipped - 3 lines]
> used for layout and to be nestable. Perhaps it is more like nested DIVs
> with CSS :-)
I must admit I find using css box model and <div> much easier it terms of
controlling positoning/size/borders/margins, but maybe it gets easier with
practice.
> If you don't like nesting layouts in this way, you can achieve the same
> visual effect by using (the notoriously difficult) GridBagLayout, (the
> popular) JGoodies FormLayout or (my favourite) MigLayout without the need
> for nesting of layouts and panels.
Maybe I'll stick with the simpler layouts for now. I think I have a slightly
better understanding now.
Thanks for your help.
Tony
Karsten Lentzsch - 03 Mar 2007 17:51 GMT
> Having got my cardlayout working (see earlier post), I am now trying to
> understand layout of components better. [...]
Some styles guides recommend to have a minimum size per button,
to ensure that users can click easily on it even if it has a
short label, for example "OK". To make it more difficult, some
platforms recommend that this minimum size changes with the font,
font size and resolution (dpi). For example on Windows a command
button should have a minimum width of 50 Dialog Units. Also,
different platforms use different button orders, for example
"OK, Cancel on Windows is "Cancel, OK" on the Mac.
I provide a free layout library that addresses all these issues
and that comes with a ButtonBarBuilder that will automatically
use the correct minimum sizes and gaps on Windows and Mac. See
http://forms.dev.java.net
The JGoodies Forms Demo is available here:
http://www.jgoodies.com/downloads/index.html
-Karsten