Results 1 to 4 of 4
  1. #1
    Andolph23 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default Use documentlistener to get source of which text field is changed

    private class myListener implements DocumentListener
    {
    public void insertUpdate(DocumentEvent e)
    {
    // Hours
    if(e.getSource() == hours)
    {
    String hours_text = hours.getText();
    System.out.println(hours_text);
    }
    }
    }


    I need to be able to tell which text field the user inserts text into, so i used e.getSource() in an if statement to find if it was the hours text field. When I compiled I got an error saying could not find symbol.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Possibly this is a scope issue and likely depending on the code that you're not showing us, but I can't really say for sure. Is hours declared in the constructor making it invisible to this inner private class?

    Bottom line: we really need more information to be able to give accurate help. Also, if you use code tags when posting code here, it will be a lot easier to read your code.

    Que tenga mucha suerte!
    Last edited by curmudgeon; 05-27-2010 at 11:01 PM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Even when you fix the compile problem is won't work, because the source is the Document, not the text field.

    So you need to pass in the text field as a paramenter to your custom class when you create it.

    Also, use the "Code tags" when you post code so the code you post retains its formatting and is readable.

  4. #4
    Andolph23 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    I was able to solve the problem using this code

    Java Code:
    private class myListener implements DocumentListener
    	{
    		public void insertUpdate(DocumentEvent e)
    		{
    			if(e.getDocument() == hours.getDocument())
    			{
    				System.out.println("Hello");
    			}
    
    		}
    but I would like to know the idea Camickr had of passing the textfield as a parameter to my custom class would work out of curiosity. I tried to use your suggestion but was having trouble.

Similar Threads

  1. Text-field templates.
    By jdipierro in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 12:48 AM
  2. Refreshing Text Field Value
    By nehakuls in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-13-2009, 11:05 AM
  3. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 PM
  4. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 05:56 AM
  5. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 04:16 PM

Posting Permissions

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