Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>there are three ways starting with easiest, ending with useless, maybe nonsense in comparing with points 1st and 2nd.</p> <ul> <li>use <code>NumberFormat.getCurrencyInstance();</code> or <code>NumberFormat.getCurrencyInstance(Locale);</code> for <code>Currency symbol</code>, then this value is valid for concrete <code>JTextField</code> / <code>JFormattedTextField</code> / <code>JSpinner</code> / <code>XxxCellRenderer</code> / <code>XxxCellEditor</code>,</li> </ul> <p>code</p> <pre><code>import java.awt.GridLayout; import java.math.RoundingMode; import java.text.NumberFormat; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.text.NumberFormatter; public class MaskFormatterTest { public static void main(String[] args) throws Exception { //NumberFormat format = NumberFormat.getNumberInstance(); NumberFormat format = NumberFormat.getCurrencyInstance(); format.setMaximumFractionDigits(2); format.setMinimumFractionDigits(2); format.setParseIntegerOnly(true); format.setRoundingMode(RoundingMode.HALF_UP); NumberFormatter formatter = new NumberFormatter(format); formatter.setMaximum(1000.0); formatter.setMinimum(0.0); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(false); JFormattedTextField tf = new JFormattedTextField(formatter); tf.setColumns(10); tf.setValue(123456789.99); JFormattedTextField tf1 = new JFormattedTextField(formatter); tf1.setValue(1234567890.99); JFormattedTextField tf2 = new JFormattedTextField(formatter); tf2.setValue(1111.1111); JFormattedTextField tf3 = new JFormattedTextField(formatter); tf3.setValue(-1111.1111); JFormattedTextField tf4 = new JFormattedTextField(formatter); tf4.setValue(-56); JFrame frame = new JFrame("Test"); frame.setLayout(new GridLayout(5, 0)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tf); frame.add(tf1); frame.add(tf2); frame.add(tf3); frame.add(tf4); frame.pack(); frame.setVisible(true); } } </code></pre> <p>.</p> <ul> <li>apply own <code>NavigationFilter</code>, fixed possition could be on the start or ending for possition (Bias) in <code>JTextField</code> / <code>JFormattedTextField</code> / <code>JSpinner</code> / <code>XxxCellRenderer</code> / <code>XxxCellEditor</code>,</li> </ul> <p>code</p> <pre><code>//@see &amp; read http://stackoverflow.com/questions/7421337/limited-selection-in-a-jtextfield-jtextcomponent import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class NavigationFilterPrefixWithBackspace extends NavigationFilter { private int prefixLength; private Action deletePrevious; public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) { this.prefixLength = prefixLength; deletePrevious = component.getActionMap().get("delete-previous"); component.getActionMap().put("delete-previous", new BackspaceAction()); component.setCaretPosition(prefixLength); } @Override public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) { fb.setDot(Math.max(dot, prefixLength), bias); } @Override public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) { fb.moveDot(Math.max(dot, prefixLength), bias); } class BackspaceAction extends AbstractAction { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) { JTextComponent component = (JTextComponent) e.getSource(); if (component.getCaretPosition() &gt; prefixLength) { deletePrevious.actionPerformed(null); } } } public static void main(String args[]) throws Exception { JTextField textField = new JTextField("Prefix_", 20); textField.setNavigationFilter(new NavigationFilterPrefixWithBackspace(7, textField)); JFrame frame = new JFrame("Navigation Filter Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(textField); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } </code></pre> <p>.</p> <ul> <li>create own <code>InputMask</code> with <code>InputVerifier</code>, workaroung could / couldn't be different for (each of) <code>JTextField</code> / <code>JFormattedTextField</code> / <code>JSpinner</code> / <code>XxxCellRenderer</code> / <code>XxxCellEditor</code>,</li> </ul>
 

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