For the Swing gurus
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).
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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 :)
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Why not a boolean check method?
public boolean check(String text)
{
try
{
Double.parseDouble(text);
return true;
}
catch
{
JOptionPane.showMessageDialog(null,"Numeric crap only");
return false;
}
return false;
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
yes, something like that :)
Thought to be a tad more friendly with my users though :p
try {
Integer.parseInt(timeout.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,
"Timeout interval must be a number",
"Input error",
JOptionPane.ERROR_MESSAGE);
return;
}
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
What do you mean your users? Are you teaching a class or something? Will however see the code?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
:lol: :lol: :lol: :lol:
I just realized what you mean't..hahaha I'm a bit slow at times.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
What do you mean your users? Are you teaching a class or something? Will however see the code?
I'm working on my SCJD exam. That means an examiner from Sun will see the code (and run it), and I hope some colleagues as friends as well if I can find people to test it first :)
And of course I work in the business, I doubt our customers would like such messages :eek:
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
I'll test it if you need me too. I'm good at trying to make things not work!
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
will keep it in mind.
Got the first draft of the network server done, now working on the client application, and then to glue them together :)
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Will this be harder than the scjp exam?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
yes, that's the idea :)
SCJP consists of learning a lot of stuff by rote and answering 60something multiple choice questions (with a rather low passing score).
SCJD consists of creating a complete client/server application from scratch including the database server and all documentation without using anything that's not part of the JDK itself (so no 3rd party or even optional Sun libraries, and some standard libraries are also not allowed, for example JDBC is explicitly disallowed).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
yes, that's the idea :)
SCJP consists of learning a lot of stuff by rote and answering 60something multiple choice questions (with a rather low passing score).
SCJD consists of creating a complete client/server application from scratch including the database server and all documentation without using anything that's not part of the JDK itself (so no 3rd party or even optional Sun libraries, and some standard libraries are also not allowed, for example JDBC is explicitly disallowed).
pffsshhhhh, easy. :lol: :lol:
Just joking, let me know how do on it.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20