Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the above answer below is a Utility method I have created to deal with above situations -</p> <pre><code> /** * Method installNumericMaximumCharacters. * * @param document * AbstractDocument * @param numberofChars * int */ public static void installNumericMaximumCharacters( AbstractDocument document, final int numberofChars) { document.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { try { if (string.equals(".") &amp;&amp; !fb.getDocument() .getText(0, fb.getDocument().getLength()) .contains(".")) { super.insertString(fb, offset, string, attr); return; } Double.parseDouble(string); if (fb.getDocument().getLength() + string.length() &lt;= numberofChars) { super.insertString(fb, offset, string, attr); } else { Toolkit.getDefaultToolkit().beep(); } } catch (Exception e) { Toolkit.getDefaultToolkit().beep(); } } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { try { if (text.equals(".") &amp;&amp; !fb.getDocument() .getText(0, fb.getDocument().getLength()) .contains(".")) { super.insertString(fb, offset, text, attrs); return; } Double.parseDouble(text); int l = fb.getDocument().getLength() - length + text.length(); if (length &gt; 0) { fb.getDocument().remove(offset, length); } if (l &lt;= numberofChars) { super.insertString(fb, offset, text, attrs); } else { Toolkit.getDefaultToolkit().beep(); } } catch (Exception e) { Toolkit.getDefaultToolkit().beep(); } } }); } </code></pre> <p>The method is bit self explanatory. When ever a change occurs on the <code>Document</code> it will evoke the <code>DocumentFilter</code>. Beware that you cannot <code>setText()</code> the same <code>Document</code>. You will have to use the <code>FilterBypass</code> object for it. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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