JaVa
   

The JTextField

The JTextField allows us to get input from the user by creating a rectangular area in which the user can type alphanumeric text (as well as symbols). Let's look at how we create a JTextField in an example app now.

Code Listing 4: Using the JTextField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextFieldExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JTextFieldExample mainApp = new JTextFieldExample();
 }
 public JTextFieldExample()
 {
 super("JTextField Example");
 setBounds(0, 0, 450, 125);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the text field...
 textfield = new JTextField(20);
 textfield.setLocation(120, 10);
 textfield.setSize(textfield.getPreferredSize());
 // Create the labels...
 label = new JLabel("Enter your name:");
 label.setLocation(10, 10);
 label.setSize(label.getPreferredSize());
 nameLabel = new JLabel("The name you entered was: ");
 nameLabel.setLocation(10, 60);
 nameLabel.setSize(nameLabel.getPreferredSize());
 // Create the button...
 button = new JButton("Update");
 button.setLocation(350, 10);
 button.setSize(button.getPreferredSize());
 // Add the action listeners
 button.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(textfield);
 getContentPane().add(label);
 getContentPane().add(nameLabel);
 getContentPane().add(button);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == button)
 {
 nameLabel.setText("The name you entered was: " +
 textfield.getText());
 nameLabel.setSize(nameLabel.getPreferredSize());
 }
 }
 JLabel label;
 JLabel nameLabel;
 JButton button;
 JTextField textfield;
}


Java End example

When you run the example program, you can see that we have created a text field that allows user input. When we click the button, the label below the text field is updated to show what the user has entered into the text field (this is an example of how we can get what the user has input into the text field). The following two screen shots display before and after the button has been clicked.

Java Click To expand
Screenshot: The JTextField example app Java Click To expand
Screenshot: The label below the JTextField is updated to show the name the user entered.

Let's now look at the code that we used to create and manipulate the JTextField. First we create the JTextField by specifying how many characters we expect the field to be able to hold in the constructor (note that the text field will scroll if the user enters more characters than the amount specified). Then we specify the size and location of the JTextField. (Note that in this example we use the setSize and setLocation methods, but we could equally use the setBounds method, as we have seen with the other GUI objects so far in this chapter.) Here is the code segment we use to create the JTextField:

textfield = new JTextField(20);
textfield.setLocation(120, 10);
textfield.setSize(textfield.getPreferredSize());


Once we have created it, we simply add it to the content pane, as we have with the other objects. This can be seen in the following line of code:

getContentPane().add(textfield);


Finally, we can see in the actionPerformed method that when the button is clicked we call the getText method of the JTextField, which simply returns a string that is the text the user has entered. This can be seen in the following line of code where we are updating the nameLabel to represent what the user has entered in our example:

nameLabel.setText("The name you entered was: " +
 textfield.getText());


JaVa
   
Comments