EditabilityExample.java Source code

Java tutorial

Introduction

Here is the source code for EditabilityExample.java

Source

/*
Core SWING Advanced Programming 
By Kim Topley
ISBN: 0 13 083292 8       
Publisher: Prentice Hall  
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;

public class EditabilityExample {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {
        }

        JFrame f = new JFrame("Editability Example");
        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
        f.getContentPane().add(firstField);

        JTextField tf = new JTextField("A read-only text field", 20);
        tf.setEditable(false);
        f.getContentPane().add(tf);

        JTextArea ta = new JTextArea("An editable\ntext area", 2, 20);
        ta.setBorder(BorderFactory.createLoweredBevelBorder());
        f.getContentPane().add(ta);

        ta = new JTextArea("A read-only\ntext area", 2, 20);
        ta.setBorder(BorderFactory.createLoweredBevelBorder());
        ta.setEditable(false);
        f.getContentPane().add(ta);

        f.pack();
        f.show();

        if (args.length == 1 && args[0].equals("disable")) {
            // Toggle the enabled state of the first
            // text field every 10 seconds
            Timer t = new Timer(10000, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    firstField.setEnabled(!firstField.isEnabled());
                    firstField.setText(firstFieldText + (firstField.isEnabled() ? "" : " (disabled)"));
                }
            });
            t.start();
        }
    }

    final static String firstFieldText = "An editable text field";

    final static JTextField firstField = new JTextField(firstFieldText, 20);

}