Home
Username:
Password:
Java 6 Tutorials

Swing Components / Text Fields & Areas

Subtitles of the Movie

There comes a time when you need to have the user input a small amount of text. It's most common for the user to be required to enter a user name and password. I'll show you a couple of components designed so you can set this up. The JTextField component will accept a single line of text typed in by the user. In this example, the prompt is for a user name. Now, this example doesn't really do anything fancy. It uses the JTextField as a component to accept the input text. The JTextField object is constructed with the number 32 as its only argument. The number is the size of the text field. In this case, the size is the number of characters, not the number of pixels. The actual size in pixels will change if you change the font. If you need to know the actual size, you will need to call get preferred size to return the number of pixels. I do that here in this example because I set the size of the window according to the size of the components. Here is the other text input component in this example. It's a special subclass of JTextField called JPasswordField. It's designed solely for the input of passwords. It has become accustomed to assume that the user is in the room with a nosey, dishonest person who would read the password if it appeared on the screen. This component works a lot like JTextField but it doesn't echo the input characters back to the screen. At the bottom of the window is a login button. None of the text input in this example is read from the components in the window until the login button is clicked. This object is added to the list of action listeners for the button. So the action performed method is called when the button is clicked. The get text method of the JTextArea is called. It returns a string object containing the user name. The get password method of the JPasswordField object is called. The password is returned as an array of chars. The documentation says this was done for security and that you should specifically erase the contents of the array when you're through with it. But I don't quite see the danger. Apparently this was done with a more primitive language in mind like C or C plus plus. In Java, all memory is erased before it's reused anyway. Here's how the program works. As you can see, the characters of the name are echoed to the screen as they are entered. The tab character takes you to the second field, but notice that the characters of the password are not echoed. But as you can see, both the name and password were output from the print statements in the program. But there are times when you need to enter more than one line of text. In that case, you need the JTextArea component, which is a rudimentary text editor allowing for the entry of blocks of text. Here you can see the creation of a new JTextArea object. It has 12 lines of text and each line has 40 columns. I don't do all the fancy stuff from this example, but you can set the font to whatever you want. You can insert the initial text if you like. You can set it to accept long lines or to automatically wrap the text. Now, it is possible to monitor the characters as they're typed in, but in this example, I have added a push button and the action performed method is called to get the text from the component. It does so by calling get text and returning a string object. Here's how it works. You can type in the text freely according to whatever the rules are that are set up. When the enter button is clicked, the entered text is displayed by the program.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available