DocumentListener for multiple Textfields

  • Follow


hi,

I wish to have a program with 10 textfields. I want to check that the
value in each text field matches the corresponding entry in an string
array of 10 elements. At the moment, i have a single text field and a
single DocumentListener to check if the text field contains the correct
value. How should I do this for multiple text fields? Should i have
multiple DocumentListeners, and if not, do do I know which text field
has issued the listener?

Thanks,

Barry.

0
Reply bg_ie (117) 10/6/2006 2:57:30 PM

bg_ie@yahoo.com wrote:
> hi,
> 
> I wish to have a program with 10 textfields. I want to check that the
> value in each text field matches the corresponding entry in an string
> array of 10 elements. At the moment, i have a single text field and a
> single DocumentListener to check if the text field contains the correct
> value. How should I do this for multiple text fields? Should i have
> multiple DocumentListeners, and if not, do do I know which text field
> has issued the listener?
> 
> Thanks,
> 
> Barry.
> 

I had an issue of validating when a button should be enabled based on a 
1 radio button in a radio set being activated, a jcombobox having data, 
as well as 2 textfields having data. I already had a custom radio button 
class that implemented ItemListener so I created a custom JComboBox 
implementing ActionListener. I then created an anonymous 
documentListener class for my 2 text fields.  All the listeners would 
set their own global boolean flag to denote when the widget had been set 
or had text entered. Since the textfields had separate document 
listeners I was able to control which flag was set (each text field had 
its own flag and they had to be set separately).

At the end of each method required for the listeners I put in a check to 
see if all flags were true and if so then I enabled my button, if not I 
would make sure its disabled.  This may not have been the best way but 
it worked.  In my case I only had 2 textfields to worry about so I was 
able to make a separate document listener for each which was the key.




In another dialog where I had over 20 textfields, I ended up creating a 
custom textfield class that implemented focusListener and when a 
textfield got focus I would set a global variable equal to the name of 
the textfield (in focusGained()) so I knew which one was currently being 
modified but you could do the same with a documentlistener instead of a 
focuslistener (document listener just triggers more events). Here is the 
code I used for that:
public void focusGained(FocusEvent e) {
	String[] tmp =
		((Component)e.getSource()).getName().split("SEP");
	selectedAttrib = tmp[0];
	selectedValue = tmp[1];
	this.setBorder(new BevelBorder (BevelBorder.RAISED));
}



Depending on your situation you could either do what I did above or you 
could combine both of my implementations by putting all your textfields 
into an array and, using a for loop, setting them up the same way and 
setup their anonymous documentlisteners individually within the loop and 
  correlate that with the array of data you have that will correspond to 
the data in the textfields.



hope this helps you
0
Reply Brandon 10/9/2006 2:49:28 AM


1 Replies
221 Views

(page loaded in 0.311 seconds)

Similiar Articles:

7/26/2012 8:06:46 PM


Reply: