Results 1 to 4 of 4

Thread: Document Listener in Java

  1. #1
    Brian Close is offline Senior Member
    Join Date
    Apr 2009
    Posts
    108
    Rep Power
    3

    Default Document Listener in Java

    I am working on live project I am using JAVA as front end and my-SQL as . I am getting confused while writing the Document Listener..!! I have tried various methods but none of them worked out. Please help by telling me how to write a Document Listener in Java? Thanks in advanced.

  2. #2
    Join Date
    May 2009
    Posts
    90
    Rep Power
    3

    Default

    I suggest you to refer following example to write a document listener in java:
    Code:
    public class DocumentEventDemo ... {
    
        textField = new JTextField(25);
        textField.addActionListener(new MyTextActionListener());
        textField.getDocument().addDocumentListener(new MyDocumentListener());
        textField.getDocument().putProperty("name", "Text Field");
    
        textArea = new JTextArea();
        textArea.getDocument().addDocumentListener(new MyDocumentListener());
        textArea.getDocument().putProperty("name", "Text Area");
        ...
    
    class MyDocumentListener implements DocumentListener {
        String newline = "\n";
     
        public void insertUpdate(DocumentEvent e) {
            updateLog(e, "inserted into");
        }
        public void removeUpdate(DocumentEvent e) {
            updateLog(e, "removed from");
        }
        public void changedUpdate(DocumentEvent e) {
        }
    
        public void updateLog(DocumentEvent e, String action) {
            Document doc = (Document)e.getDocument();
            int changeLength = e.getLength();
            displayArea.append(
                changeLength + " character" +
                ((changeLength == 1) ? " " : "s ") +
                action + doc.getProperty("name") + "." + newline +
                "  Text length = " + doc.getLength() + newline);
        }
    }
    Last edited by George kertzen; 03-17-2010 at 11:55 AM.

  3. #3
    Cecil Dumny is offline Member
    Join Date
    May 2009
    Posts
    91
    Rep Power
    3

    Default

    Methods of the DocumentListener Interface are:
    • changedUpdate(DocumentEvent) - when the style of some of the text in the listened-to document changes this method is called.
    • insertUpdate(DocumentEvent) - when text is inserted into the listened-to document this method is called.
    • removeUpdate(DocumentEvent) - when text is removed from the listened-to document this method is called.[/QUOTE]

  4. #4
    Everett Kyan is offline Member
    Join Date
    May 2009
    Posts
    86
    Rep Power
    3

    Default

    Have you tried below code? If not then try this :
    Code:
    private static void createAndShowGUI() {
    
            JFrame frm = new JFrame("DocumentEventDemo");
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JComponent newContent = new DocumentEventDemo();
            newContent.setOpaque(true); 
            frm.setContentPane(newContent);
    
            frm.pack();
            frm.setVisible(true);
        }

Similar Threads

  1. Error in PDF Document
    By PerryCollins in forum General Software Terms
    Replies: 3
    Last Post: 02-24-2010, 01:00 PM
  2. How to write a element Listener in Java?
    By Brownchris in forum Software Jargons
    Replies: 1
    Last Post: 02-13-2010, 01:56 PM
  3. How to carve a Focus Listener in Java?
    By SmithJohnson in forum Programming
    Replies: 1
    Last Post: 02-13-2010, 01:51 PM
  4. Document Sharing
    By ostin55 in forum Everything Else
    Replies: 0
    Last Post: 07-30-2008, 07:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48