Java Swing Help - Please!
Discussion
I'm trying to use a JFormattedTextField and InputVerifier to check that a user has entered a valid value. The validation works perfectly and I have written a custom event that is fired by the InputVerifier when the validation fails, the intention being that a dialog is displayed with the appropriate error message.
My problem is that if I tab away from the field everything works perfectly, however, if I use the mouse to click on another field then the InputVerifier is called twice, causing the event to be fired twice, and hence the dialog to be displayed twice. Obviously this is unaccaeptable - anyone have any idea how to fix this as it's driving me mad. It should be so simple!!
For info, here's the code snippet:
// Set up the InputVerifier.
m_textFieldVerifier = new NumberTextFieldInputVerifier();
m_textFieldVerifier.setAllowPositiveAndNegative(NumberTextFieldInputVerifier.POSITIVE);
m_textFieldVerifier.setMaximumValueAllowed(new Double(100));
m_textFieldVerifier.addValidationFailListener(this);
// Define a NumberFormat to restrict acceptable inputs.
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
Double saPercentage = m_saClientPercentage.getAllocation();
//Create the JFormattedTextField.
JFormattedTextField textField = new JFormattedTextField(nf);
textField.setInputVerifier(m_textFieldVerifier);
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setValue(saPercentage);
setTextFieldSize(textField);
panel.add(textField);
My problem is that if I tab away from the field everything works perfectly, however, if I use the mouse to click on another field then the InputVerifier is called twice, causing the event to be fired twice, and hence the dialog to be displayed twice. Obviously this is unaccaeptable - anyone have any idea how to fix this as it's driving me mad. It should be so simple!!
For info, here's the code snippet:
// Set up the InputVerifier.
m_textFieldVerifier = new NumberTextFieldInputVerifier();
m_textFieldVerifier.setAllowPositiveAndNegative(NumberTextFieldInputVerifier.POSITIVE);
m_textFieldVerifier.setMaximumValueAllowed(new Double(100));
m_textFieldVerifier.addValidationFailListener(this);
// Define a NumberFormat to restrict acceptable inputs.
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
Double saPercentage = m_saClientPercentage.getAllocation();
//Create the JFormattedTextField.
JFormattedTextField textField = new JFormattedTextField(nf);
textField.setInputVerifier(m_textFieldVerifier);
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setValue(saPercentage);
setTextFieldSize(textField);
panel.add(textField);
Thanks for trying to help but I've cracked it now thank God!
As regards an appropriate forum, fair point, but as a regular I know there are loads of IT geeks on here (me included) so there was a reasonable chance someone might know! (Just see the 'Anonymous girls on my phone thread' for definitive proof! )
As regards an appropriate forum, fair point, but as a regular I know there are loads of IT geeks on here (me included) so there was a reasonable chance someone might know! (Just see the 'Anonymous girls on my phone thread' for definitive proof! )
Gassing Station | General Gassing [Archive] | Top of Page | What's New | My Stuff