Note that there are some explanatory texts on larger screens.

plurals
  1. POCleaner way to 'sanitize' JTextField input?
    text
    copied!<p>So I'm writing the TextValueChanged handler for a JTextField that takes a long as it's input. It needs to only allow the user to type in valid values between 0 and Long.MAX. If the user types an invalid character, it should strip it, so that the value in the JTextField is always a valid long.</p> <p>My current code looks like this, but it seems ugly. With no external libs (including Apache Commons), is there a cleaner/simpler way to do this?</p> <pre><code>public void textValueChanged(TextEvent e) { if (e.getSource() instanceof JTextField) { String text = ((JTextField) e.getSource()).getText(); try { // Try to parse cleanly long longNum = Long.parseLong(text); // Check for &lt; 1 if (longNum &lt; 1) throw new NumberFormatException(); // If we pass, set the value and return setOption("FIELDKEY", longNum); } catch (NumberFormatException e1) { // We failed, so there's either a non-numeric or it's too large. String s = ((JTextField) e.getSource()).getText(); // Strip non-numeric characters s = s.replaceAll("[^\\d]", ""); long longNum = -1; if (s.length() != 0) { /* Really ugly workaround for the fact that a * TextValueChanged event can capture more than one * keystroke at a time, if it's typed fast enough, * so we might have to strip more than one * character. */ Exception e3; do { e3 = null; try { // Try and parse again longNum = Long.parseLong(s); } catch (NumberFormatException e2) { // We failed, so it's too large. e3 = e2; // Strip the last character and try again. s = s.substring(0, s.length() - 1); } // Repeat } while (e3 != null); } // We parsed, so add it (or blank it if it's &lt; 1) and return. setOption("FIELDKEY", (longNum &lt; 1 ? 0 : longNum)); } } } </code></pre> <p>The field is constantly repopulated from a getOption(key) call, so to 'store' the value, just pass it to that call.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload