Layout Problem...Please Help!

I have some code that i want to display some text field. I am using panels and have textfield and lables positioned using GridBagLayout. When i compile it does not display how i thought it should:

I want it to be like

Name -------------- Surn -------------------

Address ---------------------------------------

Address ---------------------------------------

Country -------------- Post-------------------

but it bunches it all together...please could someone help me get this right?

Thanks,

B

the code is:


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.GridBagLayout;


class practice extends JFrame
{

public practice()
{


Container contentPane = this.getContentPane();


JPanel middle = new JPanel(new BorderLayout());

JPanel middleT = new JPanel(new BorderLayout());

JPanel middleTE = new JPanel(new GridBagLayout());

JPanel middleTW = new JPanel(new GridBagLayout());


GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(2, 2, 2, 2);

c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 4;
c.weightx = c.weighty = 0.0;
middleTW.add(new JLabel("Fore"));

c.gridx = 1;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
middleTW.add(new JTextField(10), c);

c.gridx = 1;
c.gridy = 2;
c.gridwidth = 8;
c.gridheight = 1;
middleTW.add(new JTextField(10), c);

c.gridx = 2;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
middleTW.add(new JLabel("Surn"), c);

c.gridx = 0;
c.gridy = 2;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JLabel("Addr"), c);

c.gridx = 0;
c.gridy = 3;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JLabel("Addr"), c);

c.gridx = 1;
c.gridy = 3;
c.gridwidth = 8;
c.gridheight = 1;
middleTW.add(new JTextField(10), c);

c.gridx = 3;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 1;
c.ipadx = 5;
c.ipady = 0;
middleTW.add(new JTextField(10), c);

c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JLabel("Count"), c);

c.gridx = 1;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JTextField(10), c);

c.gridx = 2;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JLabel("Post"), c);

c.gridx = 3;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTW.add(new JTextField(10), c);

/////////////////////////////////////////////

c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 4;
c.weightx = c.weighty = 0.0;
middleTE.add(new JLabel("Fore"));

c.gridx = 1;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
middleTE.add(new JTextField(), c);

c.gridx = 1;
c.gridy = 2;
c.gridwidth = 8;
c.gridheight = 1;
middleTE.add(new JTextField(), c);

c.gridx = 2;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
middleTE.add(new JLabel("Surn"), c);

c.gridx = 0;
c.gridy = 2;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JLabel("Addr"), c);

c.gridx = 0;
c.gridy = 3;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JLabel("Addr"), c);

c.gridx = 1;
c.gridy = 3;
c.gridwidth = 8;
c.gridheight = 1;
middleTE.add(new JTextField(), c);

c.gridx = 3;
c.gridy = 0;
c.gridwidth = 8;
c.gridheight = 1;
c.ipadx = 5;
c.ipady = 0;
middleTE.add(new JTextField(), c);

c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JLabel("Coun"), c);

c.gridx = 1;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JTextField(), c);

c.gridx = 2;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JLabel("Post"), c);

c.gridx = 3;
c.gridy = 4;
c.gridwidth = 4;
c.gridheight = 1;
middleTE.add(new JTextField(), c);


TitledBorder middleTE1 = BorderFactory.createTitledBorder("Top");
middleTE.setBorder(middleTE1);

TitledBorder middleTW1 = BorderFactory.createTitledBorder("Top");
middleTW.setBorder(middleTW1);

middleT.add(middleTE, BorderLayout.EAST);
middleT.add(middleTW, BorderLayout.WEST);

middle.add(middleT);


contentPane.add(middle, BorderLayout.CENTER);




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

public static void main (String args[])
{
// Create frame instance
practice f = new practice();
f.setSize(950, 800);

}

}

Comments

  • [CODE][SIZE=2]import javax.swing.*;
    import java.awt.*;
    class Layout extends JFrame {
    Layout() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    String[] txt = {"Name","Surname","Address1","Address2","Country","Post"};
    JLabel[] label = new JLabel[6];
    JTextField[] field = new JTextField[6];
    int[] labelWidth = {1,1,1,1,1,1};
    int[] fieldWidth = {1,0,0,0,1,0}; // 0 = GridBagConstraints.REMAINDER
    for(int comp = 0; comp<6; comp++) {
    label[comp] = new JLabel(txt[comp], JLabel.TRAILING);
    field[comp] = new JTextField(10);
    label[comp].setLabelFor(field[comp]);
    c.gridwidth = labelWidth[comp];
    gridbag.setConstraints(label[comp],c); add(label[comp]);
    c.gridwidth = fieldWidth[comp];
    gridbag.setConstraints(field[comp],c); add(field[comp]);
    }
    }
    public static void main(String[] args) {
    Layout frame = new Layout();
    frame.setSize(350,150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }[/SIZE][/CODE]
  • : [CODE][SIZE=2]import javax.swing.*;
    : import java.awt.*;
    : class Layout extends JFrame {
    : Layout() {
    : GridBagLayout gridbag = new GridBagLayout();
    : GridBagConstraints c = new GridBagConstraints();
    : setLayout(gridbag);
    : c.weightx = 1.0;
    : c.fill = GridBagConstraints.HORIZONTAL;
    :
    : String[] txt = {"Name","Surname","Address1","Address2","Country","Post"};
    : JLabel[] label = new JLabel[6];
    : JTextField[] field = new JTextField[6];
    : int[] labelWidth = {1,1,1,1,1,1};
    : int[] fieldWidth = {1,0,0,0,1,0}; // 0 = GridBagConstraints.REMAINDER
    : for(int comp = 0; comp<6; comp++) {
    : label[comp] = new JLabel(txt[comp], JLabel.TRAILING);
    : field[comp] = new JTextField(10);
    : label[comp].setLabelFor(field[comp]);
    : c.gridwidth = labelWidth[comp];
    : gridbag.setConstraints(label[comp],c); add(label[comp]);
    : c.gridwidth = fieldWidth[comp];
    : gridbag.setConstraints(field[comp],c); add(field[comp]);
    : }
    : }
    : public static void main(String[] args) {
    : Layout frame = new Layout();
    : frame.setSize(350,150);
    : frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : frame.setVisible(true);
    : }
    : }[/SIZE][/CODE]
    :
    Thanks for this, hopefully i will be able to work from this.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories