GridBagLayout isn't sitting right

Discussion in 'Java' started by Darren, Nov 26, 2005.

  1. Darren

    Darren Guest

    Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog box.
    The dialog box contains a panel with buttons in the southe field of a
    borderlayout and the center panel contains the gridbaglayout
    the centre panel is meant to show three rows. two of whoch contain a label,
    a textfield and a button containing "..." and the other row contains a label
    and a textfield only. Sounds simple huh?

    I'm getting a mess. A self contained example is below

    Any advice would be greatly appreciated.

    -
    Darren

    --
    import java.awt.*;
    import javax.swing.event.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.*;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;


    /**
    * Summary description for JarFileFrame
    *
    */
    public class test extends JFrame
    {
    // Variables declaration


    private JPanel contentPane;
    private JPanel loOptionPanel = new JPanel();
    private JPanel loButtonPanel = new JPanel();
    JTextField ledtJarSignerPath= new JTextField();
    JTextField ledtJarSignerFormat= new JTextField();
    JTextField ledtJarVerifyFormat= new JTextField();


    JButton loOKButton=new JButton("OK");
    JButton loCancelButton=new JButton("Cancel");

    JButton loJarExeFindButton=new JButton("..");
    JButton loJarSignerExeFindButton=new JButton("..");
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    JLabel jarSignerFormatLabel =new JLabel("Jar Signer Format");




    // End of variables declaration




    test()
    {
    super("Options");

    contentPane = (JPanel)this.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.setBorder(BorderFactory.createEtchedBorder());
    contentPane.add(loOptionPanel,BorderLayout.CENTER);
    loButtonPanel.setLayout(new GridLayout());
    contentPane.add(loButtonPanel,BorderLayout.SOUTH);

    loOptionPanel.setLayout(gridbag);
    c.fill = GridBagConstraints.HORIZONTAL;

    /*c.gridwidth=5;
    c.gridheight=3;
    */
    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth=1;
    gridbag.setConstraints(jarSignerFormatLabel, c);

    loOptionPanel.add( jarSignerFormatLabel);

    c.weightx = 2;
    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth=2;
    c.fill = GridBagConstraints.HORIZONTAL;
    gridbag.setConstraints(ledtJarSignerPath, c);

    loOptionPanel.add(ledtJarSignerPath);

    c.weightx=1;
    c.gridx = 4;
    c.gridy = 0;
    c.gridwidth=1;

    gridbag.setConstraints(loJarSignerExeFindButton, c);

    loOptionPanel.add(loJarSignerExeFindButton);


    c.weightx =2;
    c.gridx = 0;
    c.gridy = 1;
    gridbag.setConstraints(jarSignerFormatLabel, c);
    loOptionPanel.add( jarSignerFormatLabel);



    c.gridx = 1;
    c.gridy = 1;
    gridbag.setConstraints(ledtJarSignerFormat, c);

    loOptionPanel.add(ledtJarSignerFormat);

    c.gridx = 0;
    c.gridy = 2;
    gridbag.setConstraints(new JLabel("Jar Verify Path"), c);
    loOptionPanel.add( new JLabel("Jar Verify Path"));

    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 2;
    gridbag.setConstraints(ledtJarVerifyFormat, c);

    loOptionPanel.add(ledtJarVerifyFormat);

    loButtonPanel.setBorder(BorderFactory.createBevelBorder(1));
    loButtonPanel.add(loOKButton);
    loButtonPanel.add(new JLabel());
    loButtonPanel.add(loCancelButton);

    loCancelButton.addActionListener
    (
    new ActionListener()
    {

    public void actionPerformed(ActionEvent e)
    {

    dispose();



    }
    }
    );
    loOKButton.addActionListener
    (
    new ActionListener()
    {

    public void actionPerformed(ActionEvent e)
    {




    dispose();



    }
    }
    );
    this.setTitle("Options");
    this.setLocation(new Point(50, 50));
    this.setSize(new Dimension(390, 150));
    this.setResizable(true);


    }

    //
    //





    public static void main(String[] args)
    {
    test mytest=new test();
    mytest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mytest.setSize(700,500);
    mytest.setLocation(50,50);
    mytest.setVisible(true);

    }





    }
    Darren, Nov 26, 2005
    #1
    1. Advertising

  2. Darren wrote:
    > Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog box.
    > The dialog box contains a panel with buttons in the southe field of a
    > borderlayout and the center panel contains the gridbaglayout
    > the centre panel is meant to show three rows. two of whoch contain a label,
    > a textfield and a button containing "..." and the other row contains a label
    > and a textfield only. Sounds simple huh?
    >
    > I'm getting a mess. A self contained example is below
    >
    > Any advice would be greatly appreciated.
    >
    > -
    > Darren


    Darren:

    You've got a mess there all right. See test1 below.

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

    public class test1 {
    public static void createGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = c.gridy = 0; c.insets = new Insets(3,3,3,3);

    JLabel l = new JLabel("Label 1");
    f.add(l,c);

    ++c.gridx;
    JTextField tf = new JTextField(10);
    f.add(tf,c);

    ++c.gridx;
    JButton b = new JButton("...");
    f.add(b,c);

    c.gridx = 0; ++c.gridy;
    l = new JLabel("Label 2");
    f.add(l,c);

    ++c.gridx;
    tf = new JTextField(10);
    f.add(tf,c);

    ++c.gridx;
    b = new JButton("...");
    f.add(b,c);

    c.gridx = 0; ++c.gridy;
    l = new JLabel("Label 3");
    f.add(l,c);

    ++c.gridx;
    tf = new JTextField(10);
    f.add(tf,c);

    f.pack();
    f.setVisible(true);
    }

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    createGUI();
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Nov 26, 2005
    #2
    1. Advertising

  3. Darren

    Roedy Green Guest


    >Darren wrote:
    >> Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog box.
    >> The dialog box contains a panel with buttons in the southe field of a
    >> borderlayout and the center panel contains the gridbaglayout
    >> the centre panel is meant to show three rows. two of whoch contain a label,
    >> a textfield and a button containing "..." and the other row contains a label
    >> and a textfield only. Sounds simple huh?
    >>
    >> I'm getting a mess. A self contained example is below
    >>
    >> Any advice would be greatly appreciated.


    see http://mindprod.com/jgloss/gridbaglayout.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 26, 2005
    #3
  4. Darren

    Darren Guest

    "Knute Johnson" <> wrote in message
    news:kZShf.1132$...
    > Darren wrote:
    > > Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog

    box.
    > > The dialog box contains a panel with buttons in the southe field of a
    > > borderlayout and the center panel contains the gridbaglayout
    > > the centre panel is meant to show three rows. two of whoch contain a

    label,
    > > a textfield and a button containing "..." and the other row contains a

    label
    > > and a textfield only. Sounds simple huh?
    > >
    > > I'm getting a mess. A self contained example is below
    > >
    > > Any advice would be greatly appreciated.
    > >
    > > -
    > > Darren

    >
    > Darren:
    >
    > You've got a mess there all right. See test1 below.


    typical of me to over complicate thangs. Thanks very much, you too Roedy
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    > public class test1 {
    > public static void createGUI() {
    > JFrame f = new JFrame();
    > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > f.setLayout(new GridBagLayout());
    >
    > GridBagConstraints c = new GridBagConstraints();
    > c.gridx = c.gridy = 0; c.insets = new Insets(3,3,3,3);
    >
    > JLabel l = new JLabel("Label 1");
    > f.add(l,c);
    >
    > ++c.gridx;
    > JTextField tf = new JTextField(10);
    > f.add(tf,c);
    >
    > ++c.gridx;
    > JButton b = new JButton("...");
    > f.add(b,c);
    >
    > c.gridx = 0; ++c.gridy;
    > l = new JLabel("Label 2");
    > f.add(l,c);
    >
    > ++c.gridx;
    > tf = new JTextField(10);
    > f.add(tf,c);
    >
    > ++c.gridx;
    > b = new JButton("...");
    > f.add(b,c);
    >
    > c.gridx = 0; ++c.gridy;
    > l = new JLabel("Label 3");
    > f.add(l,c);
    >
    > ++c.gridx;
    > tf = new JTextField(10);
    > f.add(tf,c);
    >
    > f.pack();
    > f.setVisible(true);
    > }
    >
    > public static void main(String[] args) {
    > Runnable r = new Runnable() {
    > public void run() {
    > createGUI();
    > }
    > };
    > EventQueue.invokeLater(r);
    > }
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
    Darren, Nov 26, 2005
    #4
  5. Darren

    steve Guest

    check out this

    http://madbean.com/blog/2004/17/totallygridbag.html





    On Sat, 26 Nov 2005 12:46:38 +0800, Darren wrote
    (in article <OMRhf.3174$>):

    > Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog box.
    > The dialog box contains a panel with buttons in the southe field of a
    > borderlayout and the center panel contains the gridbaglayout
    > the centre panel is meant to show three rows. two of whoch contain a label,
    > a textfield and a button containing "..." and the other row contains a label
    > and a textfield only. Sounds simple huh?
    >
    > I'm getting a mess. A self contained example is below
    >
    > Any advice would be greatly appreciated.
    >
    > -
    > Darren
    >
    > --
    > import java.awt.*;
    > import javax.swing.event.*;
    > import java.awt.event.MouseListener;
    > import java.awt.event.MouseEvent;
    > import java.awt.event.*;
    > import javax.swing.JInternalFrame;
    > import javax.swing.JPanel;
    > import javax.swing.JButton;
    > import javax.swing.JTextField;
    > import javax.swing.JLabel;
    > import javax.swing.BorderFactory;
    > import javax.swing.JFrame;
    >
    >
    > /**
    > * Summary description for JarFileFrame
    > *
    > */
    > public class test extends JFrame
    > {
    > // Variables declaration
    >
    >
    > private JPanel contentPane;
    > private JPanel loOptionPanel = new JPanel();
    > private JPanel loButtonPanel = new JPanel();
    > JTextField ledtJarSignerPath= new JTextField();
    > JTextField ledtJarSignerFormat= new JTextField();
    > JTextField ledtJarVerifyFormat= new JTextField();
    >
    >
    > JButton loOKButton=new JButton("OK");
    > JButton loCancelButton=new JButton("Cancel");
    >
    > JButton loJarExeFindButton=new JButton("..");
    > JButton loJarSignerExeFindButton=new JButton("..");
    > GridBagLayout gridbag = new GridBagLayout();
    > GridBagConstraints c = new GridBagConstraints();
    >
    > JLabel jarSignerFormatLabel =new JLabel("Jar Signer Format");
    >
    >
    >
    >
    > // End of variables declaration
    >
    >
    >
    >
    > test()
    > {
    > super("Options");
    >
    > contentPane = (JPanel)this.getContentPane();
    > contentPane.setLayout(new BorderLayout());
    > contentPane.setBorder(BorderFactory.createEtchedBorder());
    > contentPane.add(loOptionPanel,BorderLayout.CENTER);
    > loButtonPanel.setLayout(new GridLayout());
    > contentPane.add(loButtonPanel,BorderLayout.SOUTH);
    >
    > loOptionPanel.setLayout(gridbag);
    > c.fill = GridBagConstraints.HORIZONTAL;
    >
    > /*c.gridwidth=5;
    > c.gridheight=3;
    > */
    > c.weightx = 1;
    > c.gridx = 0;
    > c.gridy = 0;
    > c.gridwidth=1;
    > gridbag.setConstraints(jarSignerFormatLabel, c);
    >
    > loOptionPanel.add( jarSignerFormatLabel);
    >
    > c.weightx = 2;
    > c.gridx = 1;
    > c.gridy = 0;
    > c.gridwidth=2;
    > c.fill = GridBagConstraints.HORIZONTAL;
    > gridbag.setConstraints(ledtJarSignerPath, c);
    >
    > loOptionPanel.add(ledtJarSignerPath);
    >
    > c.weightx=1;
    > c.gridx = 4;
    > c.gridy = 0;
    > c.gridwidth=1;
    >
    > gridbag.setConstraints(loJarSignerExeFindButton, c);
    >
    > loOptionPanel.add(loJarSignerExeFindButton);
    >
    >
    > c.weightx =2;
    > c.gridx = 0;
    > c.gridy = 1;
    > gridbag.setConstraints(jarSignerFormatLabel, c);
    > loOptionPanel.add( jarSignerFormatLabel);
    >
    >
    >
    > c.gridx = 1;
    > c.gridy = 1;
    > gridbag.setConstraints(ledtJarSignerFormat, c);
    >
    > loOptionPanel.add(ledtJarSignerFormat);
    >
    > c.gridx = 0;
    > c.gridy = 2;
    > gridbag.setConstraints(new JLabel("Jar Verify Path"), c);
    > loOptionPanel.add( new JLabel("Jar Verify Path"));
    >
    > c.weightx = 0.5;
    > c.gridx = 1;
    > c.gridy = 2;
    > gridbag.setConstraints(ledtJarVerifyFormat, c);
    >
    > loOptionPanel.add(ledtJarVerifyFormat);
    >
    > loButtonPanel.setBorder(BorderFactory.createBevelBorder(1));
    > loButtonPanel.add(loOKButton);
    > loButtonPanel.add(new JLabel());
    > loButtonPanel.add(loCancelButton);
    >
    > loCancelButton.addActionListener
    > (
    > new ActionListener()
    > {
    >
    > public void actionPerformed(ActionEvent e)
    > {
    >
    > dispose();
    >
    >
    >
    > }
    > }
    > );
    > loOKButton.addActionListener
    > (
    > new ActionListener()
    > {
    >
    > public void actionPerformed(ActionEvent e)
    > {
    >
    >
    >
    >
    > dispose();
    >
    >
    >
    > }
    > }
    > );
    > this.setTitle("Options");
    > this.setLocation(new Point(50, 50));
    > this.setSize(new Dimension(390, 150));
    > this.setResizable(true);
    >
    >
    > }
    >
    > //
    > //
    >
    >
    >
    >
    >
    > public static void main(String[] args)
    > {
    > test mytest=new test();
    > mytest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > mytest.setSize(700,500);
    > mytest.setLocation(50,50);
    > mytest.setVisible(true);
    >
    > }
    >
    >
    >
    >
    >
    > }
    >
    >
    >
    steve, Nov 27, 2005
    #5
  6. Darren

    Darren Guest

    LOL, well said. :)

    "steve" <> wrote in message
    news:...
    >
    > check out this
    >
    > http://madbean.com/blog/2004/17/totallygridbag.html
    >
    >
    >
    >
    >
    > On Sat, 26 Nov 2005 12:46:38 +0800, Darren wrote
    > (in article <OMRhf.3174$>):
    >
    > > Hi all. I'm trying to use a gridbaglayout to set the layout of a dialog

    box.
    > > The dialog box contains a panel with buttons in the southe field of a
    > > borderlayout and the center panel contains the gridbaglayout
    > > the centre panel is meant to show three rows. two of whoch contain a

    label,
    > > a textfield and a button containing "..." and the other row contains a

    label
    > > and a textfield only. Sounds simple huh?
    > >
    > > I'm getting a mess. A self contained example is below
    > >
    > > Any advice would be greatly appreciated.
    > >
    > > -
    > > Darren
    > >
    > > --
    > > import java.awt.*;
    > > import javax.swing.event.*;
    > > import java.awt.event.MouseListener;
    > > import java.awt.event.MouseEvent;
    > > import java.awt.event.*;
    > > import javax.swing.JInternalFrame;
    > > import javax.swing.JPanel;
    > > import javax.swing.JButton;
    > > import javax.swing.JTextField;
    > > import javax.swing.JLabel;
    > > import javax.swing.BorderFactory;
    > > import javax.swing.JFrame;
    > >
    > >
    > > /**
    > > * Summary description for JarFileFrame
    > > *
    > > */
    > > public class test extends JFrame
    > > {
    > > // Variables declaration
    > >
    > >
    > > private JPanel contentPane;
    > > private JPanel loOptionPanel = new JPanel();
    > > private JPanel loButtonPanel = new JPanel();
    > > JTextField ledtJarSignerPath= new JTextField();
    > > JTextField ledtJarSignerFormat= new JTextField();
    > > JTextField ledtJarVerifyFormat= new JTextField();
    > >
    > >
    > > JButton loOKButton=new JButton("OK");
    > > JButton loCancelButton=new JButton("Cancel");
    > >
    > > JButton loJarExeFindButton=new JButton("..");
    > > JButton loJarSignerExeFindButton=new JButton("..");
    > > GridBagLayout gridbag = new GridBagLayout();
    > > GridBagConstraints c = new GridBagConstraints();
    > >
    > > JLabel jarSignerFormatLabel =new JLabel("Jar Signer Format");
    > >
    > >
    > >
    > >
    > > // End of variables declaration
    > >
    > >
    > >
    > >
    > > test()
    > > {
    > > super("Options");
    > >
    > > contentPane = (JPanel)this.getContentPane();
    > > contentPane.setLayout(new BorderLayout());
    > > contentPane.setBorder(BorderFactory.createEtchedBorder());
    > > contentPane.add(loOptionPanel,BorderLayout.CENTER);
    > > loButtonPanel.setLayout(new GridLayout());
    > > contentPane.add(loButtonPanel,BorderLayout.SOUTH);
    > >
    > > loOptionPanel.setLayout(gridbag);
    > > c.fill = GridBagConstraints.HORIZONTAL;
    > >
    > > /*c.gridwidth=5;
    > > c.gridheight=3;
    > > */
    > > c.weightx = 1;
    > > c.gridx = 0;
    > > c.gridy = 0;
    > > c.gridwidth=1;
    > > gridbag.setConstraints(jarSignerFormatLabel, c);
    > >
    > > loOptionPanel.add( jarSignerFormatLabel);
    > >
    > > c.weightx = 2;
    > > c.gridx = 1;
    > > c.gridy = 0;
    > > c.gridwidth=2;
    > > c.fill = GridBagConstraints.HORIZONTAL;
    > > gridbag.setConstraints(ledtJarSignerPath, c);
    > >
    > > loOptionPanel.add(ledtJarSignerPath);
    > >
    > > c.weightx=1;
    > > c.gridx = 4;
    > > c.gridy = 0;
    > > c.gridwidth=1;
    > >
    > > gridbag.setConstraints(loJarSignerExeFindButton, c);
    > >
    > > loOptionPanel.add(loJarSignerExeFindButton);
    > >
    > >
    > > c.weightx =2;
    > > c.gridx = 0;
    > > c.gridy = 1;
    > > gridbag.setConstraints(jarSignerFormatLabel, c);
    > > loOptionPanel.add( jarSignerFormatLabel);
    > >
    > >
    > >
    > > c.gridx = 1;
    > > c.gridy = 1;
    > > gridbag.setConstraints(ledtJarSignerFormat, c);
    > >
    > > loOptionPanel.add(ledtJarSignerFormat);
    > >
    > > c.gridx = 0;
    > > c.gridy = 2;
    > > gridbag.setConstraints(new JLabel("Jar Verify Path"), c);
    > > loOptionPanel.add( new JLabel("Jar Verify Path"));
    > >
    > > c.weightx = 0.5;
    > > c.gridx = 1;
    > > c.gridy = 2;
    > > gridbag.setConstraints(ledtJarVerifyFormat, c);
    > >
    > > loOptionPanel.add(ledtJarVerifyFormat);
    > >
    > > loButtonPanel.setBorder(BorderFactory.createBevelBorder(1));
    > > loButtonPanel.add(loOKButton);
    > > loButtonPanel.add(new JLabel());
    > > loButtonPanel.add(loCancelButton);
    > >
    > > loCancelButton.addActionListener
    > > (
    > > new ActionListener()
    > > {
    > >
    > > public void actionPerformed(ActionEvent e)
    > > {
    > >
    > > dispose();
    > >
    > >
    > >
    > > }
    > > }
    > > );
    > > loOKButton.addActionListener
    > > (
    > > new ActionListener()
    > > {
    > >
    > > public void actionPerformed(ActionEvent e)
    > > {
    > >
    > >
    > >
    > >
    > > dispose();
    > >
    > >
    > >
    > > }
    > > }
    > > );
    > > this.setTitle("Options");
    > > this.setLocation(new Point(50, 50));
    > > this.setSize(new Dimension(390, 150));
    > > this.setResizable(true);
    > >
    > >
    > > }
    > >
    > > //
    > > //
    > >
    > >
    > >
    > >
    > >
    > > public static void main(String[] args)
    > > {
    > > test mytest=new test();
    > > mytest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > > mytest.setSize(700,500);
    > > mytest.setLocation(50,50);
    > > mytest.setVisible(true);
    > >
    > > }
    > >
    > >
    > >
    > >
    > >
    > > }
    > >
    > >
    > >

    >
    >
    Darren, Nov 28, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yasunari
    Replies:
    9
    Views:
    548
  2. Tom Parson
    Replies:
    1
    Views:
    502
    Daniel Dyer
    May 23, 2006
  3. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    urllib2 - My POST Request just isn't working right

    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Feb 1, 2006, in forum: Python
    Replies:
    1
    Views:
    3,793
    Don't Code Faster Than You Can See
    Feb 1, 2006
  4. Eric Nieuwland
    Replies:
    8
    Views:
    501
    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
    Feb 1, 2006
  5. Shot (Piotr Szotkowski)

    a Hash and a Set (sitting in a tree)

    Shot (Piotr Szotkowski), Jan 24, 2007, in forum: Ruby
    Replies:
    6
    Views:
    145
    Shot (Piotr Szotkowski)
    Jan 27, 2007
Loading...

Share This Page