Note that there are some explanatory texts on larger screens.

plurals
  1. POMaskFormatter with Regex
    primarykey
    data
    text
    <p>I'm trying to make a textfield that is set to accept floats and I am using regex for this.. I'm having a problem because when I input the number for example 12345.67 then when I try to erase the input I cant erase the number 1.. why?></p> <pre><code>import javax.swing.*; import javax.swing.text.*; public class DecimalFormatter extends DefaultFormatter { protected java.util.regex.Matcher matcher; public DecimalFormatter(java.util.regex.Pattern regex) { setOverwriteMode(false); matcher = regex.matcher(""); } public Object stringToValue(String string) throws java.text.ParseException { if (string == null) return null; matcher.reset(string); if (! matcher.matches()) throw new java.text.ParseException("does not match regex", 0); return super.stringToValue(string); } public static void main(String argv[]) { JLabel lab1 = new JLabel("Decimal:"); java.util.regex.Pattern Decimal = java.util.regex.Pattern.compile("^[1-9]\\d{0,4}([.]\\d{0,2})?$", java.util.regex.Pattern.CASE_INSENSITIVE); DecimalFormatter decimalFormatter = new DecimalFormatter(Decimal); decimalFormatter.setAllowsInvalid(false); JFormattedTextField ftf1 = new JFormattedTextField(decimalFormatter); JFrame f = new JFrame("DecimalFormatter Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pan1 = new JPanel(new java.awt.BorderLayout()); pan1.add(lab1, java.awt.BorderLayout.WEST); pan1.add(ftf1, java.awt.BorderLayout.CENTER); lab1.setLabelFor(ftf1); f.getContentPane().add(pan1, java.awt.BorderLayout.SOUTH); f.setSize(300, 80); f.setVisible(true); } } </code></pre>
    singulars
    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.
 

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