Re: GridBagLayout not behaving as specified



"Ian Wilson" <scobloke2@xxxxxxxxxxxxx> wrote in message news:462732e8$0$10724$db0fefd9@xxxxxxxxxxxxxxxxx

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class GBTest extends JFrame {

private JPanel panel = new JPanel();
private JLabel userLabel = new JLabel("User:");
private JLabel passwordLabel = new JLabel("Password:");
private JLabel dbLabel = new JLabel("Database:");
private JTextField userTextField = new JTextField();
private JTextField dbTextField = new JTextField();
private JPasswordField passwordField = new JPasswordField(8);

private JButton dbButton = new JButton("...");

public GBTest() {
setLayout(new BorderLayout());
panel.setLayout(new GridBagLayout());

userLabel.setText("User:");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(0, 4, 18, 0);
gbc.ipadx = 0;
panel.add(userLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 4, 18, 4);
gbc.ipadx = 40;
panel.add(userTextField, gbc);

gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(0, 4, 18, 0);
gbc.ipadx = 0;
panel.add(passwordLabel, gbc);

gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 4, 18, 8);
gbc.ipadx = 0;
panel.add(passwordField, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(0, 4, 18, 0);
gbc.ipadx = 0;
panel.add(dbLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 10;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 4, 18, 0);
gbc.ipadx = 0;
panel.add(dbTextField, gbc);

gbc.gridx = 4;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.insets = new Insets(0, 4, 18, 8);
gbc.ipadx = 0;
panel.add(dbButton, gbc);

add(panel, BorderLayout.CENTER);

pack();
setPreferredSize(new Dimension(400, 250));
setSize(new Dimension(400, 250));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new GBTest().setVisible(true);
}
});
}
}

Hmm, not quite there but closer - thanks. Perhaps GridBagLayout is not the answer.

--
And loving it,

-Q
_________________________________________________
Qu0llSixFour@xxxxxxxxx
(Replace the "SixFour" with numbers to email me)

.


Quantcast