Results 1 to 6 of 6
  1. #1
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default How do I receive notifications when the user stops typing into a JTextField?

    Sorry for my bad English.

    I create a simple text editor using a JTextArea.
    I have a FindWord action and an HighLightAll action.
    The user types a word into a JTextField.
    If a JToggleButton (HighLightAll On-Off) is selected then the matches marked.

    How do I receive notifications when the user stops typing the word into the
    JTextField?

    I attach a DocumentListener to the document of JTextField.
    The problem is that for each character that the user enters into the JTextField,
    HighlightAllAction is called.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How do I receive notifications when the user stops typing into a JTextField?

    Quote Originally Posted by kosmos890 View Post
    I attach a DocumentListener to the document of JTextField.
    The problem is that for each character that the user enters into the JTextField,
    HighlightAllAction is called.
    Only if that's what you coded it to do.

    You could start a javax.swing.Timer and reset() it each time the document is updated; the Timer's actionPerformed(...) would trigger the highlighting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: How do I receive notifications when the user stops typing into a JTextField?

    I'm not certain you can unless you check for a termination character and can get it in raw mode (characters are available as typed). I say this because what indicates that a person has stopped typing? If they type really slowly, like one character every two seconds, they are still typing. What about one character every 10 seconds?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: How do I receive notifications when the user stops typing into a JTextField?

    @DarrylBurke
    Thanks for your reply
    Swing.Timer is a good idea. It works good for me.

    @jim829
    I agree with your comments. Timer gives me a functionality similar to that I had imagined.

    Java Code:
    public class MyPanel extends JPanel{
    
    	private Timer timer;
        private JTextField jTextField1;
    
    	public MyPanel(){
    			
        timer = new Timer(1000, new ActionListener() {
                
    	   public void actionPerformed(ActionEvent e) {
    		  highlightAll();
                }
            });		
    
          jTextField1.getDocument().addDocumentListener(new DocumentListener() {
    
    		 public void insertUpdate(DocumentEvent e) {
    
    			if (timer.isRunning()) {
    		          timer.stop();
                }
               
    			 timer.start();
              }
    				
    				/*
    				 *
    				 */
    	  }
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How do I receive notifications when the user stops typing into a JTextField?

    Quote Originally Posted by kosmos890 View Post
    Java Code:
    		 public void insertUpdate(DocumentEvent e) {
    
    			if (timer.isRunning()) {
    		          timer.stop();
                }
               
    			 timer.start();
              }
    Apologies for writing reset() earlier, there isn't a reset() method. The method to use is restart().

    And it's really a bad idea to mix spaces and tabs for indenting code. Two to four spaces are usually preferred for forum posts.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,732
    Rep Power
    5

    Default Re: How do I receive notifications when the user stops typing into a JTextField?

    I use 4 spaces myself (because tabs are just... insanity when sharing code with multiple people) but I must admit that tabs tend to slip in when I copy/paste/adjust stuff from other sources. Its hard to spot half of the time since they're invisible unless you turn on the code polluting whitespace markers.

    Nowadays I make Eclipse do a source format when saving a file (preferences -> java -> editor -> save actions), that helps a lot.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 6
    Last Post: 02-25-2012, 04:35 AM
  2. Replies: 9
    Last Post: 06-16-2010, 03:42 AM
  3. Replies: 4
    Last Post: 03-25-2009, 04:54 AM
  4. Replies: 0
    Last Post: 12-02-2008, 10:55 AM

Posting Permissions

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