Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

button columns

Thread view: 
Paul Sorenson - 31 Dec 2003 21:59 GMT
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);
 }
}
A. Bolmarcich - 01 Jan 2004 00:44 GMT
> Greetings,
>
[quoted text clipped - 4 lines]
> 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]
Karsten Lentzsch - 01 Jan 2004 19:16 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.