| | |
For the Swing gurus
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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).
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.
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)
private void dumpNonNumericInput(KeyEvent e) { char input = e.getKeyChar(); String oldText = ((JTextField)e.getSource()).getText(); if (input != KeyEvent.VK_BACK_SPACE && input != KeyEvent.VK_DELETE && (input < '0' || input > '9')) { JOptionPane.showMessageDialog(this, "Numeric input required", "Input error", JOptionPane.ERROR_MESSAGE); e.consume(); ((JTextField)e.getSource()).setText(oldText); } }
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.
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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.
if(stringvar.matches("\\d")){}
Not sure why removing a showMessageDialog would have any effect on the following code.
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
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
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
-Sam Keen, from To Love and Be Loved
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Why not a boolean check method?
Java Syntax (Toggle Plain Text)
public boolean check(String text) { try { Double.parseDouble(text); return true; } catch { JOptionPane.showMessageDialog(null,"Numeric crap only"); return false; } return false; }
yes, something like that 
Thought to be a tad more friendly with my users though :p

Thought to be a tad more friendly with my users though :p
Java Syntax (Toggle Plain Text)
try { Integer.parseInt(timeout.getText()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Timeout interval must be a number", "Input error", JOptionPane.ERROR_MESSAGE); return; }
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Other Threads in the Java Forum
- Previous Thread: Running Tomcat on a webserver?
- Next Thread: how to create application
Views: 3862 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows






