Document ElementIterator Demo : Document Event « Swing JFC « Java






Document ElementIterator Demo

Document ElementIterator Demo
   
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;

public class ElementSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Element Example");
    Container content = frame.getContentPane();

    final JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);

    JButton button = new JButton("Show Elements");
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Document document = textArea.getDocument();
        ElementIterator iterator = new ElementIterator(document);
        Element element = iterator.first();
        while (element != null) {
          System.out.println(element.getStartOffset());
          element = iterator.next();
        }
      }
    };
    button.addActionListener(actionListener);

    content.add(scrollPane, BorderLayout.CENTER);
    content.add(button, BorderLayout.SOUTH);

    frame.setSize(250, 250);
    frame.setVisible(true);
  }
}

           
         
    
    
  








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.React to the document update insert changed eventReact to the document update insert changed event
5.HTMLDocument: Document Iterator Example
6.DocumentListener DemoDocumentListener Demo
7.extends PlainDocument to create a float value type text field
8.extends PlainDocument to create alpha and numeric value based field field
9.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
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.