Using Text Fields

The JTextField allows us to get input from the user by creating a rectangular area in which the user can enter text. Let's look at how we create a JTextField in an example application now:

Code Listing 13-2: Using the JTextField

start example
import java.awt.*; import java.awt.event.*; import javax.swing.*;   public class JTextFieldExample extends JFrame      implements ActionListener <>     public JTextFieldExample()     <>         super("JTextField Example");         setBounds(0, 0, 450, 125);         getContentPane().setLayout(null);         setResizable(false);         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());         <>     <>         public static void main(String[] args)     <>         JTextFieldExample mainApp = new JTextFieldExample();     <>          JLabel label;     JLabel nameLabel;     JButton button;     JTextField textfield; <>
end example

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

click to expand
Figure 13-2: The JTextField Example application

click to expand
Figure 13-3: The label below the JTextField is updated to show the name the user entered into the JTextField.

Let's now look at the code we used to create and manipulate the JTextField. First we create the JTextField by specifying how many characters you expect the field to be able to hold (visually, that is, for sizing purposes) in the constructor (note that the text field will scroll if the user enters more characters than can be displayed. 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 JButton example.) 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());

Here we start to see one of the problems with using the Swing GUI system when we are looking from a games point of view. The JTextField grabs the focus of the keyboard input, and hence the game no longer would have control of the user input until the JTextField releases it.

Note 

In the last example, if we added an action listener to the textfield object and then handled an action event for this object in the actionPerformed method as we did for the Update button, this would update the label when the user presses Return after entering text into the text field.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net