Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CCannot get the two rows to align properly

 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
//setSize(800,600);
setLayout(new GridLayout(2,2));
//Change the color of the name field
/*nameTextField.setBackground(Color.black);
nameTextField.setForeground(Color.cyan);
nameTextField.setCaretColor(Color.cyan);*/
//Change the font of the name field
Font font=new Font("SansSerif",Font.BOLD|Font.ITALIC,16);
nameTextField.setFont(font);
//Add a Tool Tip
nameTextField.setToolTipText("Enter your Name");
//Add the name and age textfields
add(nameTextField);
add(nameLabel);
//Change the color of the Age field
/*ageTextField.setBackground(Color.darkGray);
ageTextField.setForeground(Color.yellow);
ageTextField.setCaretColor(Color.yellow);*/
//Enter a tooltip
ageTextField.setToolTipText("Enter your age");
//set the font
ageTextField.setFont(font);
//Add the agelabel and age textfield
add(ageLabel);
add(ageTextField);
}
public static void main(String[] args)
{
//Create a frame object
JFrame frame=new JFrame("JTextField Example!");
"LabelExample.java" 95 lines, 2552 characters
LabelExample example=new LabelExample();
frame.getContentPane().add(example,BorderLayout.CENTER);
frame.setSize(240,90);
frame.setVisible(true);
//Create a window listener
frame.addWindowListener( new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
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++)>
{
if(Character.isDigit(strSource[i]))
{
strDestination[nDestinationIndex++]=strSource[i];
}
else
{
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
//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
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi hemal,
I've exchanged the order of two lines in your program .now it should work fine.


regards
deeksha
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply deekasha. It worked!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic