grid bag layout doesnt work?
i want the size of the buttons, lbls and text fields to increase as the form is enlarged
but my code doesnt do this can anyone help>?
GridBagConstraints c =new GridBagConstraints();
public Mess()
{
// set the structure of the Jframe
this.setTitle("Room");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//create a jpannel for the main buttons and log in text fields
jPanel =new JPanel(new GridBagLayout());
jPanel.setVisible(true);
this.getContentPane().add(jPanel, BorderLayout.NORTH);
//create the labels, textboxes and buttons on the main pannel the the top of the GUI
lblUserName =new JLabel("User Name: ");
lblUserName.setVisible(true);
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
jPanel.add(lblUserName, c);
txtUserName =new JTextField("", 10);
txtUserName.setVisible(true);
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
jPanel.add(txtUserName, c);
[1521 byte] By [
h1400046a] at [2007-11-26 15:05:31]

It works, just not the way you think it should.You misunderstand the layout. Go read how it really works.%
i thought that c.weightx = 0.5;caused it to change it?
In the future, Swing related questions should be posted in the Swing forum.[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use GridBag Layout[/url].
yes sorry, still no understanding of why at all
Well personally I never use GridBayLayout because its too complex for me. But you have a better chance of someone helping if you actually post demo code.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
GridBagLayout is an easy girl to tame.
Its easier if you break up the tasks into methods.
public void makeLayout(){
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
CONTAINER.setLayout(gridbag);
constraints.fill = GridBagConstraints.BOTH;
addComponent(panel, component, 1, 1, 1, 1, 1, 1);
}
public void addComponent(gridbag, constraints, container, component, int, int, int, int, double, double){
buildConstraints(constraints, 1,1,1,1,1,1);
gridbag.setConstraints(COMPONENT, constraints);
CONTAINER.add(COMPONENT);
}
public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, double wx, double wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public class setInsets(GridBagConstraints constraints, int t, int b, int l, int r){
constraints.insets.top = t;
constraints.insets.bottom = b;
constraints.insets.left = l;
constraints.insets.right = r;
}
hi
CONTAINER.setLayout(gridbag);
constraints.fill = GridBagConstraints.BOTH;
addComponent(panel, component, 1, 1, 1, 1, 1, 1);
what is CONTAINER, component?
my code is:
GridBagConstraints c = new GridBagConstraints();
public Mess()
{
// set the structure of the Jframe
this.setTitle("Room");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//create a jpannel for the main buttons and log in text fields
jPanel = new JPanel(new GridBagLayout());
jPanel.setVisible(true);
this.getContentPane().add(jPanel, BorderLayout.NORTH);
lblUserName = new JLabel("User Name: ");
lblUserName.setVisible(true);
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
jPanel.add(lblUserName, c);
txtUserName = new JTextField("", 10);
txtUserName.setVisible(true);
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
jPanel.add(txtUserName, c);
lblPassword = new JLabel("Password: ");
lblPassword.setVisible(true);
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
jPanel.add(lblPassword, c);
txtPassword = new JTextField("", 10);
txtPassword.setVisible(true);
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 0;
jPanel.add(txtPassword, c);
btnLogOn = new JButton(logOnAction);
c.weightx = 0.5;
c.gridx = 4;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
jPanel.add(btnLogOn, c);
btnLogOff = new JButton(logOffAction);
c.weightx = 0.5;
c.gridx = 5;
c.gridy = 0;
jPanel.add(btnLogOff, c);
btnWhis = new JButton("Whis");
c.weightx = 0.5;
c.gridx = 6;
c.gridy = 0;
jPanel.add(btnWhis, c);
btnWhisaddActionListener(this);
btnWhis.setEnabled(false);
jPanelUsers = new JPanel(new GridBagLayout());
jPanelUsers.setVisible(true);
this.getContentPane().add(jPanelUsers, BorderLayout.EAST);
lblUserList = new JLabel("Logged On Users:");
lblUserList.setVisible(true);
c.weightx = 0.5;
c.gridx = 6;
c.gridy = 1;
jPanelUsers.add(lblUserList, c);
modelList = new DefaultListModel();
dataList = new JList(modelList);
dataList.setBackground(Color.WHITE);
c.weightx = 0.5;
c.gridx = 8;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
jPanelUsers.add(dataList, c);
jPanelMess = new JPanel(new GridBagLayout());
jPanelMess.setVisible(true);
this.getContentPane().add(jPanelMess, BorderLayout.SOUTH);
lblMess = new JLabel("Mess: ");
lblMess.setVisible(true);
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
jPanelMess.add(lblMess, c);
txtMess = new JTextField("", 50);
txtMess.setVisible(true);
txtMess.setEditable(false);
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
jPanelMess.add(txtMess, c);
btnSend = new JButton("Send");
btnSend.setEnabled(false);
btnSend.addActionListener(this);
c.weightx = 0.5;
c.gridx = 3;
c.gridy = 3;
c.fill = GridBagConstraints.NONE;
jPanelMess.add(btnSend, c);
jtxtC = new JTextArea(10,5);
JScrollPane sp = new JScrollPane(jtxtC);
this.add(sp);
// set the style and format of the Jframe
this.setBackground(Color.white);
this.pack();
this.setSize(700, 500);
this.setVisible(true);
Your code is not a SSCCE. Why do people feel they need to write 100 lines of code to test something they don't understand.
Create a simple program with a couple of components and play with the constraints. Then when you understand the basic concepts you incorporate it back into your real code
container = JPanel
component = whatever components youre adding
it wasnt usable code but an example of how to actually utilize
some object oriented design to make GBL easier.
your code is insane.
when you have pages of blocks of code that are all identical you
know you have a problem.
you also dont have to set setVisible on components, by the way.
also if all of your weights are .5 then they are all actually 1.
.5 means half of the size of whatever component is 1.
its like a percentage.
ok well i know this works by changing the weight between re-size and not:
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
button = new JButton("Button 1");
//c.weightx = 0.5;
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
//Display the window.
frame.pack();
frame.setVisible(true);
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class GBLDemo{
public static void main(String[] args) {
new GBLDemo();
}
public GBLDemo(){
makeLayout();
}
public void makeLayout(){
frame = new JFrame("GBL Demo");
panel = new JPanel();
button = new JButton("BUTTON");
slider = new JSlider();
checkbox = new JCheckBox("CHECKBOX");
textfield = new JTextField("TEXTFIELD");
setLayout(panel);
setFill(GridBagConstraints.BOTH);
setInsets(5, 5, 5, 5);
setComponent(button, 10, 10, 1, 1, 1, 1);
setComponent(slider, 20, 10, 1, 1, 1, 1);
setComponent(checkbox, 10, 20, 1, 1, .7, .5);
setComponent(textfield, 20, 20, 1, 1, .3, .5);
frame.setContentPane(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// PUT THE FOLLOWING BLOCK INTO A STATIC UTILITY CLASS
public void setLayout(JComponent container){
gridbag = new GridBagLayout();
constraints = new GridBagConstraints();
this.container = container;
this.container.setLayout(gridbag);
}
public void buildConstraints(int gx, int gy, int gw, int gh, double wx, double wy){
constraints.gridx = gx;
constraints.gridy = gy;
constraints.gridwidth = gw;
constraints.gridheight = gh;
constraints.weightx = wx;
constraints.weighty = wy;
}
public void setInsets(int top, int bottom, int left, int right){
constraints.insets.top = top;
constraints.insets.bottom = bottom;
constraints.insets.left = left;
constraints.insets.right = right;
}
public void setFill(int fill){
constraints.fill = fill;
}
public void setComponent(JComponent component, int gx, int gy, int gw, int gh, double wx, double wy){
buildConstraints(gx, gy, gw, gh, wx, wy);
gridbag.setConstraints(component, constraints);
container.add(component);
}
// END BLOCK
JFrame frame;
JPanel panel;
JButton button;
JSlider slider;
JCheckBox checkbox;
JTextField textfield;
// UTILITY CLASS VARIABLES
static JComponent container;
static GridBagLayout gridbag;
static GridBagConstraints constraints;
// END BLOCK
}
Maybe this will get you started.
Man, I sure wish teachers actually spend time on breaking up
problems.
import java.awt.*;
import javax.swing.*;
public class Mess {
public Mess(final Container container) {
container.setLayout(new BorderLayout());
container.add(Mess.buildLoginPanel(), BorderLayout.NORTH);
container.add(Mess.buildUserPanel(), BorderLayout.EAST);
container.add(Mess.buildControlPanel(), BorderLayout.SOUTH);
final JTextArea messages = new JTextArea(10, 5);
final JScrollPane messageScrollPane = new JScrollPane(messages);
container.add(messageScrollPane, BorderLayout.CENTER);
}
private static JPanel buildLoginPanel() {
final JPanel loginPanel = new JPanel(new GridBagLayout());
final GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
loginPanel.add(new JLabel("User Name: "), gbc);
final JTextField userName = new JTextField("", 10);
gbc.gridx++;
loginPanel.add(userName, gbc);
gbc.gridx++;
loginPanel.add(new JLabel("Password: "), gbc);
final JPasswordField password = new JPasswordField("", 10);
gbc.gridx++;
loginPanel.add(password, gbc);
final JButton logon = new JButton("Log On");
gbc.fill = GridBagConstraints.NONE;
gbc.gridx++;
loginPanel.add(logon, gbc);
final JButton logoff = new JButton("Log Off");
gbc.gridx++;
loginPanel.add(logoff, gbc);
final JButton whis = new JButton("Whis");
whis.setEnabled(false);
gbc.gridx++;
loginPanel.add(whis, gbc);
return loginPanel;
}
private static JPanel buildUserPanel() {
final JPanel userPanel = new JPanel(new GridBagLayout());
final GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
userPanel.add(new JLabel("Logged On Users:"), gbc);
final DefaultListModel userModel = new DefaultListModel();
final JList users = new JList(userModel);
users.setBackground(Color.WHITE);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy++;
userPanel.add(users, gbc);
return userPanel;
}
private static JPanel buildControlPanel() {
final JPanel controlPanel = new JPanel(new GridBagLayout());
final GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
controlPanel.add(new JLabel("Mess: "), gbc);
final JTextField textMessage = new JTextField("", 50);
textMessage.setEditable(false);
gbc.gridx++;
controlPanel.add(textMessage, gbc);
final JButton send = new JButton("Send");
send.setEnabled(false);
gbc.gridx++;
controlPanel.add(send, gbc);
return controlPanel;
}
public static void main(final String[] argv) {
final JFrame frame = new JFrame("Room");
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
new Mess(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
