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 2005

Tip: Looking for answers? Try searching our database.

GridBagLayout: need help!

Thread view: 
Lou Lipnickey - 18 Jan 2005 22:19 GMT
I am trying to build a 1 line JPanel with a mix of Labels and buttons of
varying sizes. I was trying to do it with GridBagLayout but it appears
that GridBag loses its variable column spanning abilities with a single
row. Below is a sample program. If it is run "as is", two buttons will
appear even sized, even though one is directed to span 2 columns. If you
uncomment the commented block containing 3 additional buttons, the 2
buttons appear with their desired sizes (one bigger than the other) but
the additional 3 buttons appear. Any help is appreciated in getting this
or another solution to work . THanks - Lou

import java.awt.*;
import javax.swing.*;

public class Test_GridBag1 {

    public static void addComponentsToPane(JPanel pane) {

        JButton button;
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
   /*
    button = new JButton("Button 1");
    c.gridx = 0;
        c.gridy = 0;
    c.gridwidth=1;
        pane.add(button, c);

        button = new JButton("Button 2");
        c.gridx = 1;
        c.gridy = 0;
    //c.gridwidth=2;
        pane.add(button, c);

        button = new JButton("Button 3");
        c.gridx = 2;
        c.gridy = 0;
    c.gridwidth=1;
        pane.add(button, c);
    */

    button = new JButton("4.5");
        c.anchor = GridBagConstraints.EAST;
        c.gridx = 0;
        c.gridwidth = 1;   //2 columns wide
        c.gridy = 1;       //third row
    c.anchor = GridBagConstraints.EAST; //bottom of space
       
        pane.add(button, c);
        button = new JButton("5");
        c.weightx = 1.0;   //request any extra vertical space
        c.gridx = 1;       //aligned with button 2
        c.gridwidth = 2;   //2 columns wide
        c.gridy = 1;
    c.anchor = GridBagConstraints.EAST;//third row
    pane.add(button, c);

    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("GridBagLayoutDemo");
    JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
        addComponentsToPane(panel);
    frame.getContentPane().add( panel );

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Larry Barowski - 18 Jan 2005 23:46 GMT
> I am trying to build a 1 line JPanel with a mix of Labels and buttons of
> varying sizes. I was trying to do it with GridBagLayout but it appears
> that GridBag loses its variable column spanning abilities with a single
> row. Below is a sample program. If it is run "as is", two buttons will
> appear even sized, even though one is directed to span 2 columns.

The number of columns spanned does not directly control
the column width. If you want the preferred width of the
second button to be double what it would normally be, then
do this:

  button = new JButton("5") {
        public Dimension getPreferredSize() {
           Dimension ps = super.getPreferredSize();
           ps.width *= 2;
           return ps; } };

If you want it to be exactly double the width of the first,
then use a final variable to hold the first button and do this:

  button = new JButton("5") {
        public Dimension getPreferredSize() {
           Dimension ps = button1.getPreferredSize();
           ps.width *= 2;
           return ps; } };

But you probably don't want to do either of those. For most
layouts you will have either a vertical or horizontal line of
buttons, all of which you want to be the same width - the
preferred width of the widest one. GridBagLayout will work
for a column of same-width buttons, but not, in any nice
way, for a row of same-width buttons.
Lou Lipnickey - 18 Jan 2005 23:56 GMT
figures .... thanks - Lou

>>I am trying to build a 1 line JPanel with a mix of Labels and buttons of
>>varying sizes. I was trying to do it with GridBagLayout but it appears
[quoted text clipped - 28 lines]
> for a column of same-width buttons, but not, in any nice
> way, for a row of same-width buttons.


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.