Java Reference
In-Depth Information
The same set of 53 actions is available for all text components.
JTextField
,
JFormattedTextField
, and
JPasswordField
have one extra action, called
notify-field-accept
,
used for detecting when the Enter key is pressed while in the text component. The
JFormattedTextField
has a second extra action,
reset-field-edit
, for when the contents don't
validate against the provided format mask.
JTextPane
adds its own set of about 20 more actions
for dealing with multiple-attributed text.
Listing 16-1 shows the source used to generate Figure 16-1. The
RadioButtonUtils
class
was created in Chapter 5.
Listing 16-1.
Listing Text Component Actions
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ListActions {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("TextAction List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String components[] = {
"JTextField", "JFormattedTextField", "JPasswordField",
"JTextArea", "JTextPane", "JEditorPane"};
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Determine which component selected
String command = actionEvent.getActionCommand();
JTextComponent component = null;
if (command.equals("JTextField")) {
component = new JTextField();
} else if (command.equals("JFormattedTextField")) {
component = new JFormattedTextField();
} else if (command.equals("JPasswordField")) {
component = new JPasswordField();