Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add textfields properly 1

Status
Not open for further replies.

yigit

Technical User
Jun 28, 2006
14
0
0
TR
Im really sorry to bother you guys with these questions. I tried to make 2 textfields one on top of the other reading name and last name, (I tried to put labels defining each textfield but gave problem) with two buttons insert and delete at the bottom. This code puts all in one line...Why?thank you in advance.

public class DBGUIMain extends JFrame {
final static boolean RIGHT_TO_LEFT = false;
final static boolean SHOULD_FILL = true;
final static boolean SHOULD_WEIGHT_X = true;

JButton Insert,Delete;
JTextField firstNameTextField, lastNameTextField;
public DBGUIMain() {
super("TEST");
JLabel label1, label2;
label1 = new JLabel("First Name:");
label2 = new JLabel("Last Name:");

if (RIGHT_TO_LEFT) {
getContentPane().setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
firstNameTextField = new JTextField("", 15);
lastNameTextField = new JTextField("", 15);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

if (SHOULD_FILL) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (SHOULD_WEIGHT_X) {
c.weightx = 1.0;
c.ipady = 10;
c.ipadx = 10;
}
c.gridx = 0;
c.gridy = 0;
//add(label1); gives prob here!
getContentPane().add(firstNameTextField);

c.gridx = 0;
c.gridy = 1;
//add(label2); and here
getContentPane().add(lastNameTextField);

Insert = new JButton(" INSERT ");
c.gridx = 2;
c.gridy = 4;
getContentPane().add(Insert, c);
Insert.addMouseListener(new MyMouseListener());

Delete = new JButton(" DELETE ");
c.gridx = 3;
c.gridy = 4;
getContentPane().add(Delete, c);
Delete.addMouseListener(new MyMouseListener());

}

public static void main(String argv[]) {

JFrame.setDefaultLookAndFeelDecorated(true);
DBGUIMain frame = new DBGUIMain();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if (evt.getSource().equals(Insert)) {
//do insert
}
}
}
}
 
You need to create a separate GridBagConstraints object for each component addition. So in your case, create four, one for each component location.

Tim
 
From the top of my head, change x and y for the second textfield to be 1 and 0. That should add it in the second row.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top