943,965 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4376
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2005
0

For the Swing gurus

Expand Post »
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).

Java Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2005
0

Re: For the Swing gurus

Can you do it with a try catch statement?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ProFreelance is offline Offline
17 posts
since Aug 2005
Aug 5th, 2005
0

Re: For the Swing gurus

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Aug 5th, 2005
0

Re: For the Swing gurus

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, 22 views)
File Type: java TextFieldDemo.java (280 Bytes, 14 views)
Reputation Points: 10
Solved Threads: 0
Light Poster
cheenu78 is offline Offline
45 posts
since Jun 2005
Aug 5th, 2005
0

Re: For the Swing gurus

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
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2005
0

Re: For the Swing gurus

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Aug 5th, 2005
0

Re: For the Swing gurus

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2005
0

Re: For the Swing gurus

Why not a boolean check method?
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Aug 5th, 2005
0

Re: For the Swing gurus

yes, something like that
Thought to be a tad more friendly with my users though :p

Java Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2005
0

Re: For the Swing gurus

What do you mean your users? Are you teaching a class or something? Will however see the code?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Running Tomcat on a webserver?
Next Thread in Java Forum Timeline: how to create application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC