React to the document update insert changed event : Document Event « Swing JFC « Java






React to the document update insert changed event

React to the document update insert changed event
   

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class DocumentListenerDemo extends JFrame implements DocumentListener {
  private JTextField hourField = new JTextField("12", 3);

  private JTextField minuteField = new JTextField("00", 3);

  private JLabel label = new JLabel();

  public DocumentListenerDemo() {
    setTitle("TextTest");
    setSize(500, 200);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();

    JPanel p = new JPanel();

    p.add(hourField);
    hourField.getDocument().addDocumentListener(this);

    p.add(minuteField);
    minuteField.getDocument().addDocumentListener(this);

    contentPane.add(p, "Center");
    contentPane.add(label, "North");

  }

  public void insertUpdate(DocumentEvent e) {
    label.setText(e.toString());
  }

  public void removeUpdate(DocumentEvent e) {
    label.setText(e.toString());
  }

  public void changedUpdate(DocumentEvent e) {
  }

  public static void main(String[] args) {
    JFrame frame = new DocumentListenerDemo();
    frame.show();
  }

}


           
         
    
    
  








Related examples in the same category

1.DocumentFilter that maps lowercase letters to uppercaseDocumentFilter that maps lowercase letters to uppercase
2.An extension of PlainDocument that restricts the length of its contentAn extension of PlainDocument that restricts the length of its content
3.Document Event DemoDocument Event Demo
4.HTMLDocument: Document Iterator Example
5.DocumentListener DemoDocumentListener Demo
6.extends PlainDocument to create a float value type text field
7.extends PlainDocument to create alpha and numeric value based field field
8.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
9.Document ElementIterator DemoDocument ElementIterator Demo
10.Format JTextField's text to uppercase
11.Overriding a Few Default Typed Key Bindings in a JTextComponent
12.Overriding - key
13.Override $ key
14.Overriding space key
15.Disable a character so that no action is invoked.
16.Bind a keystroke to shift-space
17.Prevent the space from being inserted when shift-space is pressed.
18.Subclass InputVerifier
19.extends PlainDocument
20.This Document restricts the size of the contained plain text to the given number of characters.
21.A frame with three text fields to set the background color.