aspose file tools*
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
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
Author

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");
//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

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


regards
deeksha
Hemal Mehta
Ranch Hand

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