Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > GridBagLayout isn't sitting right

Reply
Thread Tools

GridBagLayout isn't sitting right

 
 
Darren
Guest
Posts: n/a
 
      11-26-2005
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.createEtchedBo rder());
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.createBevelB order(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_CLO SE);
mytest.setSize(700,500);
mytest.setLocation(50,50);
mytest.setVisible(true);

}





}



 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-26-2005
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/
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-26-2005

>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.
 
Reply With Quote
 
Darren
Guest
Posts: n/a
 
      11-26-2005

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:kZShf.1132$(E-Mail Removed)...
> 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/



 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      11-27-2005

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$(E-Mail Removed)>):

> 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.createEtchedBo rder());
> 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.createBevelB order(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_CLO SE);
> mytest.setSize(700,500);
> mytest.setLocation(50,50);
> mytest.setVisible(true);
>
> }
>
>
>
>
>
> }
>
>
>



 
Reply With Quote
 
Darren
Guest
Posts: n/a
 
      11-28-2005
LOL, well said.

"steve" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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$(E-Mail Removed)>):
>
> > 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.createEtchedBo rder());
> > 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.createBevelB order(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_CLO SE);
> > mytest.setSize(700,500);
> > mytest.setLocation(50,50);
> > mytest.setVisible(true);
> >
> > }
> >
> >
> >
> >
> >
> > }
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RIGHT-justifying a RadioButton (in GridBagLayout) ? Tom Parson Java 1 05-23-2006 09:56 AM
Sitting 70-228 tomorrow... www.pocketpcheaven.com MCSE 10 03-06-2006 06:26 PM
Question about tables; "sitting" flush against the side of the browser Yasunari HTML 9 11-13-2004 02:42 PM
Sitting behind a local pix501 and can't access an external site with Pix501 from Cisco VPN CLient- why? simon Cisco 1 09-21-2004 12:52 PM
Urgent help reqd! - Cisco router sitting in front of hardware firewall Kapamarou Cisco 5 01-02-2004 01:19 AM



Advertisments