GridBagLayout isn't sitting right

D

Darren

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);

}





}
 
K

Knute Johnson

Darren said:
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);
}
}
 
D

Darren

Knute Johnson said:
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);
}
}
 
S

steve

check out this

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





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);

}





}
 
D

Darren

LOL, well said. :)

steve said:
check out this

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





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);

}





}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top