Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:37:49+00:00 2026-06-04T03:37:49+00:00

I am trying to implement a Document Listener in my program. So far every

  • 0

I am trying to implement a Document Listener in my program. So far every time the user types in a new word, I am getting the whole text and saving it. What I want to do is to get only the new word/words typed in and process them. Can you give me suggestions how I can do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:37:50+00:00Added an answer on June 4, 2026 at 3:37 am

    Below is proposed code similar to yours above:

    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.util.Arrays;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    
    
    //public class DocumentListener {       // Fix: not a good name for the class since it is part of JVM
      public class DocumentEventDemo extends JFrame {   // easier than extends JPanel
          JPanel txtPanel, centerPanel;
          GridLayout grid;
    
          JTextField txtField;
          JTextArea txtArea;
          JFrame frame;
          JComponent newContentPane;
    
          FlowLayout flow;
    
          public DocumentEventDemo() {
    
              txtArea = new JTextArea();
              txtArea.getDocument().addDocumentListener(new MyDocumentListener());
              txtArea.getDocument().putProperty("txtArea", "MyDocumentListener");
    
    //        txtField = new JTextField(10);    // 10 chars max
    //        txtField.setText("12345");
    
              centerPanel = new JPanel();
              grid = new GridLayout(2,1,1,1);
    
              txtPanel = new JPanel();
              flow = new FlowLayout(FlowLayout.CENTER);
              txtPanel.setLayout(flow);
    
              //Adding control GUI fields to the only panel
              // txtPanel.add(txtArea);
              // txtPanel.add(txtField);
    
    
              // Forming the center view with GUI controls
              centerPanel.setLayout(grid);
    //        centerPanel.add(txtPanel);
              centerPanel.add(txtArea);
    
              // Add Panels to the Frame
              getContentPane().add(centerPanel,"Center");
    
              this.setSize(500,200);
              this.validate();
              this.setVisible(true);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //        newContentPane = getRootPane();
    //        newContentPane.setOpaque(true);
    //        frame.setContentPane(newContentPane);
    
    }
    
        //MyEditor editor = new MyEditor(); // what is MyEditor?
    
        //javax.swing.event.DocumentListener mydocumentListener = new javax.swing.event.DocumentListener()
    
        // Make a class to define the inherited abstract methods, which are also events.
        class MyDocumentListener implements DocumentListener {
           String[] word=new String[50];
           String text; 
           int i=0;
           int y;
           int l;
           int len;
    
           public void changedUpdate(DocumentEvent documentEvent) {
               System.out.println("The text has been changed.");
           }
    
           public void insertUpdate(DocumentEvent documentEvent) {
               try {
                   GetWord(documentEvent);
               } catch (BadLocationException ex) {
                   Logger.getLogger(DocumentListener.class.getName()).log(Level.SEVERE, null, ex);
               }
           }
    
           public void removeUpdate(DocumentEvent documentEvent) {
               System.out.println("A character has been removed!");
           }
    
          private void GetWord(DocumentEvent documentEvent) throws BadLocationException {
              //get the type of event
              DocumentEvent.EventType type = documentEvent.getType();
              //check what is the event, hence what is the user doing  
              if (type.equals(DocumentEvent.EventType.INSERT)) {
                  Document source = documentEvent.getDocument();
    
                  y=documentEvent.getOffset();
                  l=documentEvent.getLength();
                  len=source.getLength();
    
                  text = source.getText(y,l);
                  if(text.equals(" "))
                  {
                      for (int z=0;z<len;z++)
                      {
                          System.out.print(word[z]);
                      }
                      System.out.println("\n");
                      Arrays.fill(word,null); 
                      i=0;
                  }
    
                  word[i]=text;
                  i++;
              } else  {
                  System.out.println("A character has been removed!");
              } 
          }
        };  // DocumentListener class instantiation
    
    
    //  editor. editArea.getDocument().addDocumentListener(mydocumentListener);
    
    
        public static void main(String args[]){
            new DocumentEventDemo();
        }
    
    }   // TOP class
    

    Notes:

    • My most outer class extends JFrame, which creates the window & listeners the easy way, I think.
    • DocumentEventDemo is a constructor that creates the UI controls and looks.
    • I created a class that implements DocumentListener. This way I can override the abstract events.
    • My main function is on the bottom inside the class DocumentEventDemo, like yours actually.
    • I do not see the code for class MyEditor. Therefore I replaced that with JTextArea, behaves like an editor.
    • Your code GetWord seems to be working well. Congratulations!
    • Your technique of using System.out.println is not working since the app is windows GUI application instead of console, which works well with System.out.
    • Obviously, you still have work to do with the Listener functions for changed and remove.

    Have fun!

    Tommy Kwee

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to implement a custom checkout policy for a Plone Document Management
Warm-up I'm trying to come up with a good way to implement customized document
I am trying to implement the winnowing algorithm for document fingerprinting in R. Here
I'm writing a program trying to implement a toy XML processor. Right now the
I've been trying to implement a commenting system for my Rails app. Every event
I'm trying to implement the following code in a html document: $(function () {
I am trying to implement a delegate method for my Cocoa document-based application which
I am trying to implement the composite specification pattern, as per the Specifications Document
I'm trying to implement a page with a choice of user's preferences in an
So I'm trying to implement the evercookie on a cakePHP website, but I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.