This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes CCannot get the two rows to align properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CCannot get the two rows to align properly" Watch "CCannot get the two rows to align properly" New topic

CCannot get the two rows to align properly

Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Following is a simple code which creates two textfields. When I run the program I cannot get the two texfields to align one below the other. One comes on the left and other on right.Can someone point out the problems:
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class LabelExample extends JPanel
JLabel nameLabel=new JLabel("Name",JLabel.RIGHT);
JLabel ageLabel=new JLabel("Age",JLabel.RIGHT);
JTextField nameTextField=new JTextField(40);
JTextField ageTextField=new JTextField(new NumberDocument(),"",20);
//Put the constructor here
public LabelExample()
//setTitle("Example of Crew name and age field");
setLayout(new GridLayout(2,2));
//Change the color of the name field
//Change the font of the name field
Font font=new Font("SansSerif",Font.BOLD|Font.ITALIC,16);
//Add a Tool Tip
nameTextField.setToolTipText("Enter your Name");
//Add the name and age textfields
//Change the color of the Age field
//Enter a tooltip
ageTextField.setToolTipText("Enter your age");
//set the font
//Add the agelabel and age textfield
public static void main(String[] args)
//Create a frame object
JFrame frame=new JFrame("JTextField Example!");
"" 95 lines, 2552 characters
LabelExample example=new LabelExample();
//Create a window listener
frame.addWindowListener( new WindowAdapter()
public void WindowClosing(WindowEvent e)
protected class NumberDocument extends PlainDocument
public void insertString(int offs,String str,AttributeSet a) throws BadLocationException
//Get the characters to insert into the document
char[] strSource=str.toCharArray();
//Create a buffer to copy the results into
char[] strDestination=new char[strSource.length];
int nDestinationIndex=0;
//Loop through all the characters in source string
for(int i=0;i<strSource.length;i++)>
//Create a string out of destination buffer
String strInsertString=new String(strDestination,0,nDestinationIndex);
//Call the PlainDocument insertString method
super.insertString(offs, strInsertString,a);
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi hemal,
I've exchanged the order of two lines in your program .now it should work fine.

Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Thanks for the reply deekasha. It worked!
I agree. Here's the link:
subject: CCannot get the two rows to align properly
Similar Threads
JComboBox not working properly on a GridBagLayout ??
Fixed size JTextField
what am i doing wrong urgent
Need to convert Console App to GUI...
How to set paddings?