Results 1 to 8 of 8
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Attaching listeners to array of components does not perform as expected.

    Hello,
    I am trying to initialize a build with 5 text fields wit different checks but somehow automate it, so I`ve created a local array of these components and initialized them with a loop like that:

    Java Code:
    for (frameCount = 0; frameCount < 2; frameCount++ ) {
    	entry[frameCount].getDocument().addDocumentListener(
    	new DocumentListener() {
    		public void removeUpdate(DocumentEvent e) {
    			if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
    			else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");								
    			}
    		public void changedUpdate(DocumentEvent e) {
    			if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
    			else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");							
    			}
    		public void insertUpdate(DocumentEvent e) {
    			if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
    			else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");			
    			}
    		}); //end set
    		inputWorker.add(labels[frameCount]);
    		inputWorker.add(entry[frameCount]);	
    											}	
    	for (; frameCount < 5; frameCount++  ) {
    		entry[frameCount].getDocument().addDocumentListener(
    		new DocumentListener() {
    			public void removeUpdate(DocumentEvent e) {
    			     if ( checkField(entry[frameCount]) ) System.err.println("OK !");
    			else System.err.println("ERROR, FLOAT EXPECTED");									
    			}
    		        public void changedUpdate(DocumentEvent e) {
    				if ( checkField(entry[frameCount]) ) System.err.println("OK !");
    				else System.err.println("ERROR, FLOAT EXPECTED");	
    			}
    			public void insertUpdate(DocumentEvent e) {
    				if ( checkField(entry[frameCount]) ) System.err.println("OK !");
    				else System.err.println("ERROR, FLOAT EXPECTED");					
    			}
    		}); //end set
    		inputWorker.add(labels[frameCount]);
    		inputWorker.add(entry[frameCount]);	
    	}
    	// bad C habbit - a global										
    	frameCount = 0;
    So... How is that the loop does not attach the listeners to all elements? I mean the check is performed for the first array element from there on all checks are valid... this is not right. Or my approach is not right here? Please help. ( And please no answers with "DON`T USE GLOBALS", it`s a temp solution FGS ).

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Attaching listeners to array of components does not perform as expected.

    If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing. We can't really tell what's going on with your code from a contextless snippet. Note that this does not mean posting your whole project- boil the problem down to as few lines as possible, and we'll be happy to help.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Attaching listeners to array of components does not perform as expected.

    You don't need to create a new DocumentListener for every Document. Create 1 listener and add the that listener to each component.

    Post your Short, Self Contained, Correct Example that demonstrates the problem.

  4. #4
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Attaching listeners to array of components does not perform as expected.

    @camickr - really, how to do it? A separate class? Or there is another anonymous method?

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

    Default Re: Attaching listeners to array of components does not perform as expected.

    You can use a separate class or an anonymous inner class the way you currently are doing it:

    Java Code:
    DocumentListener listener = new DocumentListener()
    {
            public void removeUpdate(DocumentEvent e) {
                if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
                else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");                               
                }
            public void changedUpdate(DocumentEvent e) {
                if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
                else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");                           
                }
            public void insertUpdate(DocumentEvent e) {
                if ( checkLength(entry[frameCount], 3) ) System.err.println("OK !");
                else System.err.println("ERROR, NAME MUST BE MORE THAN 3 CHARS");           
    };
    
    for (frameCount = 0; frameCount < 2; frameCount++ ) 
    {
        entry[frameCount].getDocument().addDocumentListener( listener );
        inputWorker.add(labels[frameCount]);
        inputWorker.add(entry[frameCount]); 
    }
    This will not fix your problem. It just minimizes the classes that you create.

    An even better solution is to create an inner class and then specify the number of characters instead of hardcoding 3.

  6. #6
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Attaching listeners to array of components does not perform as expected.

    Thanks... it didn`t fix the problem but I`ve added check for everything in every if satement like that:

    Java Code:
    public void removeUpdate(DocumentEvent e) {
    	if ( ( checkLength(entry[frameCount], 3) &&
    		checkLength(entry[1], 3 ) ) && 
    		( checkField(entry[2]) &&
    	          checkField(entry[3]) &&
    		checkField(entry[4]) ) ) System.err.println("OK !");
    Kind of ugly... but requres all 5 fields to be filled correctly... Really works, your idea made me create an array of DocumentEvents... not bad... made the program faster!

  7. #7
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Attaching listeners to array of components does not perform as expected.

    Error above the first checkLength is for entry 0 in the array not changing from the loop. But I guess you`d figured it :)

  8. #8
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Attaching listeners to array of components does not perform as expected.

    Actually bothering all with my issues there is finally an expected result.
    Attached Thumbnails Attached Thumbnails Attaching listeners to array of components does not perform as expected.-notpassed.png   Attaching listeners to array of components does not perform as expected.-passed.png  

Similar Threads

  1. Class expected error with array
    By zlloyd1 in forum New To Java
    Replies: 2
    Last Post: 12-12-2012, 06:26 AM
  2. Removing components from an array
    By shivam0101 in forum New To Java
    Replies: 5
    Last Post: 09-20-2012, 11:56 PM
  3. multi-dimensional array not printing as expected
    By jubbiejub in forum New To Java
    Replies: 8
    Last Post: 05-05-2012, 11:19 AM
  4. Replies: 3
    Last Post: 04-19-2011, 06:21 PM
  5. Replies: 11
    Last Post: 03-02-2011, 04:33 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
  •