For the Swing gurus

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

For the Swing gurus

 
0
  #1
Aug 4th, 2005
yes, there's an area of Java I'm almost a complete novice in (so far, I'm going full steam ahead :cheesy: ).

I've the following code to filter out non-numeric input from a JTextField (which is meant to contain a timeout interval in minutes, that's why).

  1. private void dumpNonNumericInput(KeyEvent e) {
  2. char input = e.getKeyChar();
  3. String oldText = ((JTextField)e.getSource()).getText();
  4. if (input != KeyEvent.VK_BACK_SPACE && input != KeyEvent.VK_DELETE &&
  5. (input < '0' || input > '9')) {
  6. JOptionPane.showMessageDialog(this, "Numeric input required",
  7. "Input error",
  8. JOptionPane.ERROR_MESSAGE);
  9. e.consume();
  10. ((JTextField)e.getSource()).setText(oldText);
  11.  
  12. }
  13. }

This method works like a charm, that's not the problem.
The problem is that is I remove the error message popup (just the showMessageDialog command) the method no longer removes the faulty input either.
I THINK it's something to do with Swing thread timing, but I'm not sure.
Neither do I know how to cure this, which is more important at the moment as I'd rather not have that popup appear every time someone makes a typo.

I could probably subclass JTextField but that may be more trouble than it's worth.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 17
Reputation: ProFreelance is an unknown quantity at this point 
Solved Threads: 0
ProFreelance ProFreelance is offline Offline
Newbie Poster

Re: For the Swing gurus

 
0
  #2
Aug 5th, 2005
Can you do it with a try catch statement?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Re: For the Swing gurus

 
0
  #3
Aug 5th, 2005
A better alternative would be to disable text input altogether (call setEditable(false) on the text field. This deals with the problem of people putting in garbage input. Then all you need to do is use regular expression on the character that was input. Then use reg exp for input
if(stringvar.matches("\\d")){}

Not sure why removing a showMessageDialog would have any effect on the following code.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 45
Reputation: cheenu78 is an unknown quantity at this point 
Solved Threads: 0
cheenu78's Avatar
cheenu78 cheenu78 is offline Offline
Light Poster

Re: For the Swing gurus

 
0
  #4
Aug 5th, 2005
hi jwenting,

I have attached a Textfield which takes number of columns(maximum number of characters that the textfield can take) as parameter. If you use the usual textfields with keylistener, you can set illegal characters programattically. This textfield(one attached) does not allow illegal characters either by entry or progammatically. You can run the program attached and check out and tell me whether it was useful.

TextFieldDemo is the one with main method.

I am not sure whether this is exactly your requirement

regards
Srinivas
Attached Files
File Type: java NumericTextField.java (1.7 KB, 9 views)
File Type: java TextFieldDemo.java (280 Bytes, 6 views)
We come to love not by finding a perfect person, but by learning to see an imperfect person perfectly.

-Sam Keen, from To Love and Be Loved
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: For the Swing gurus

 
0
  #5
Aug 5th, 2005
Cheers, I'll give it a look.

Gink, the idea is to allow only certain input. If it were readonly I'd have used a label or something else instead
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

Re: For the Swing gurus

 
0
  #6
Aug 5th, 2005
Im not suggesting to make it read-only. What im saying is add a keylistener and whenever the user presses a key just update the textfield accordingly. The only reason I said uneditable is to prevent people from typing in characters that the keylistener doesnt respond to. It is still editable by the program, and the user wont know it's uneditable.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: For the Swing gurus

 
0
  #7
Aug 5th, 2005
ah.

Found I can't catch shortcut keys (like Alt-T, Alt-X, etc.) using this system so I abandoned it.
Instead I now validate the field when it's value is read out instead of when it's input and give an error then.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: For the Swing gurus

 
0
  #8
Aug 5th, 2005
Why not a boolean check method?
  1. public boolean check(String text)
  2. {
  3. try
  4. {
  5. Double.parseDouble(text);
  6. return true;
  7. }
  8. catch
  9. {
  10. JOptionPane.showMessageDialog(null,"Numeric crap only");
  11. return false;
  12. }
  13. return false;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: For the Swing gurus

 
0
  #9
Aug 5th, 2005
yes, something like that
Thought to be a tad more friendly with my users though :p

  1. try {
  2. Integer.parseInt(timeout.getText());
  3. } catch (NumberFormatException ex) {
  4. JOptionPane.showMessageDialog(this,
  5. "Timeout interval must be a number",
  6. "Input error",
  7. JOptionPane.ERROR_MESSAGE);
  8. return;
  9. }
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: For the Swing gurus

 
0
  #10
Aug 5th, 2005
What do you mean your users? Are you teaching a class or something? Will however see the code?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 3862 | Replies: 17
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC