Listening to Text Components Events with a DocumentListener : JTextComponent « Swing « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing » JTextComponent 
14. 14. 16. Listening to Text Components Events with a DocumentListener
Listening to Text Components Events with a DocumentListener
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class AddingDocumentListenerJTextFieldSample {
  public static void main(String args[]) {
    final JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    DocumentListener documentListener = new DocumentListener() {
      public void changedUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void insertUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void removeUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      private void printIt(DocumentEvent documentEvent) {
        DocumentEvent.EventType type = documentEvent.getType();
        String typeString = null;
        if (type.equals(DocumentEvent.EventType.CHANGE)) {
          typeString = "Change";
        }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
          typeString = "Insert";
        }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
          typeString = "Remove";
        }
        System.out.print("Type : " + typeString);
        Document source = documentEvent.getDocument();
        int length = source.getLength();
        System.out.println("Length: " + length);
      }
    };
    textField.getDocument().addDocumentListener(documentListener);
    
    frame.setSize(250150);
    frame.setVisible(true);
  }
}
14. 14. JTextComponent
14. 14. 1. JTextComponent: the parent class for all the components used as textual views.
14. 14. 2. Drawing in the Background of a ComponentDrawing in the Background of a Component
14. 14. 3. Loading and Saving ContentLoading and Saving Content
14. 14. 4. Accessing the System ClipboardAccessing the System Clipboard
14. 14. 5. Default Editor Kit: cutActionDefault Editor Kit: cutAction
14. 14. 6. Register Keyboard action: registerKeyboardActionRegister Keyboard action: registerKeyboardAction
14. 14. 7. CaretListener Interface and CaretEvent ClassCaretListener Interface and CaretEvent Class
14. 14. 8. Restricting Caret Movement: NavigationFilterRestricting Caret Movement: NavigationFilter
14. 14. 9. Remove Key action from Text componentRemove Key action from Text component
14. 14. 10. Listening to Text Components Events with an ActionListenerListening to Text Components Events with an ActionListener
14. 14. 11. Listening to JTextField Events with an KeyListenerListening to JTextField Events with an KeyListener
14. 14. 12. Share Document
14. 14. 13. Listening to Text Components Events with an InputVerifierListening to Text Components Events with an InputVerifier
14. 14. 14. TextAction Name Constants
14. 14. 15. Using Text Component ActionsUsing Text Component Actions
14. 14. 16. Listening to Text Components Events with a DocumentListenerListening to Text Components Events with a DocumentListener
14. 14. 17. Text Component PrintingText Component Printing
14. 14. 18. GIF Writer
14. 14. 19. Text component supports both cut, copy and paste (using the DefaultEditorKit's built-in actions) and drag and drop
14. 14. 20. Overriding the Default Action of a JTextComponent
14. 14. 21. Modifying Text in a JTextComponent: Insert some text at the beginning
14. 14. 22. Modifying Text in a JTextComponent: Insert some text after the 5th character
14. 14. 23. Modifying Text in a JTextComponent: Append some text
14. 14. 24. Modifying Text in a JTextComponent: Delete the first 5 characters
14. 14. 25. Modifying Text in a JTextComponent: Replace the first 3 characters with some text
14. 14. 26. Limiting the Capacity of a JTextComponent
14. 14. 27. Filter all editing operations on a text component
14. 14. 28. Enabling Text-Dragging on a JTextComponent
14. 14. 29. Enumerating All the Views in a JTextComponent
14. 14. 30. Sharing a Document Between JTextComponents
14. 14. 31. Highlight a word in JTextComponent
14. 14. 32. Remove Highlighting in a JTextComponent
14. 14. 33. Listening for Caret Movement Events in a JTextComponent
14. 14. 34. Listening for Editing Changes in a JTextComponent
14. 14. 35. Moving the Caret of a JTextComponent
14. 14. 36. Setting the Blink Rate of a JTextComponent's Caret
14. 14. 37. Using the Selection of a JTextComponent
14. 14. 38. Better way to set the selection
14. 14. 39. Set the color behind the selected text
14. 14. 40. Set the caret color
14. 14. 41. Document and DocumentFilter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.