Greetings,
I"m trying to create a column of buttons that have equal width and with each
button having preferred height. Below is an example I modified slightly
from a 1999 comp.lang.java.programmer post. I could use
JComponent.setMaximumSize on each button, but that seems like too much work
for such a common layout. Is there an easy way to make the buttons, "Button
1", "Button long" and "Button 3" have equal width?
Thanks
-Paul
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.border.*;
import java.beans.*;
public class Layout1 extends JPanel
{
public Layout1()
{
JPanel labelPanel = new JPanel();
labelPanel.setBorder(BorderFactory.createEtchedBorder());
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
labelPanel.add(new JLabel("Label 1"));
labelPanel.add(new JLabel("Label 2"));
labelPanel.add(Box.createHorizontalGlue());
labelPanel.add(new JLabel("Label 3"));
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(new JButton("Button 1"));
buttonPanel.add(new JButton("Button long"));
buttonPanel.add(new JButton("Button 3"));
buttonPanel.add(Box.createVerticalGlue());
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
panel.setLayout(new BorderLayout());
panel.add(BorderLayout.CENTER, new JLabel("Remaining space"));
setLayout(new BorderLayout());
add(BorderLayout.NORTH, labelPanel);
add(BorderLayout.WEST, buttonPanel);
add(BorderLayout.CENTER, panel);
}
static public void main(String args[])
{
JFrame frame = new JFrame("Layout Test");
frame.getContentPane().add(BorderLayout.CENTER, new Layout1());
frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent we) { System.exit(1); }});
frame.setSize(400, 300);
frame.setVisible(true);
}
}
|
|
0
|
|
|
|
Reply
|
Paul
|
12/31/2003 9:59:47 PM |
|
In article <3ff346d5$1_3@127.0.0.1>, Paul Sorenson wrote:
> Greetings,
>
> I"m trying to create a column of buttons that have equal width and with each
> button having preferred height. Below is an example I modified slightly
> from a 1999 comp.lang.java.programmer post. I could use
> JComponent.setMaximumSize on each button, but that seems like too much work
> for such a common layout. Is there an easy way to make the buttons, "Button
> 1", "Button long" and "Button 3" have equal width?
Use a different layout manager. Recently, in comp.lang.java.program there
is a thread with the subject "Easy Layout Question" where.GridBagLayout was
used. In your sample program replace the lines that set the layout and
contents of buttonPanel with
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER; // layout one per row
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0; // stretch components horizontally
buttonPanel.add(new JButton("Button 1"), gbc);
buttonPanel.add(new JButton("Button long"), gbc);
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1.0; // add extra space below last button
buttonPanel.add(new JButton("Button 3"), gbc);
You may not find this to be any easier than invoking setMaximumSize on
each button.
[helpful sample program snipped]
|
|
0
|
|
|
|
Reply
|
aggedor
|
1/1/2004 12:44:05 AM
|
|
Paul Sorenson wrote:
> I"m trying to create a column of buttons that have equal width and with each
> button having preferred height. [...]
The JGoodies FormLayout can layout well designed
button bars and button stacks. In addition to this
layout manager, the Forms layout system ships with
builder classes that specialize in building button
bar and button stacks that comply with style guides.
On top of the layout manager and builders, factory
classes can vend predesigned button bars for frequently
used bars - that shall further reduce the time
necessary to implement typical layouts and it increases
the consistency.
FormLayout can implement button bars and button stacks
that most layout managers, including the GridBagLayout
and combinations of GridBagLayout with GridLayout
cannot implement. For example, the following bar:
http://www.jgoodies.com/freeware/formsdemo/images/bar.png
Three buttons have the same width, one button is
larger and has narrow margins on its sides. The gap
between "OK" and "Cancel" is a logical gap that has
been requested from the platform's style guide.
"OK", "Cancel" and "Help" are a little bit wider
than their preferred size. They honor a minimum size
of 50 dlu. These dialog units shrink and grow with
the font, font size and screen resolution.
The live Forms Demo provides more examples.
The Forms layout framework is open source and
available at no charge. The project is hosted
at JavaDesktop.org where you can download the
library, sources, a demo, a tutorial and the
tutorial sources, see http://forms.dev.java.net/
Screenshots and more examples are available at
http://www.jgoodies.com/freeware/forms/
Hope this helps,
Karsten Lentzsch :: JGoodies :: Java User Interface Design
|
|
0
|
|
|
|
Reply
|
Karsten
|
1/1/2004 7:16:44 PM
|
|
|
2 Replies
143 Views
(page loaded in 0.021 seconds)
|