I'm having problems with masks in JFormattedTextField
I understand that it replaces invalid characters with a space, or whatever you define with setPlaceholderCharacter, but what I need is to allow deletion or backspace, and DO NOT insert a space instead of the character that I deleted until the rest of string allowed in mask.
For example, with a mask:, the *#*****string is "12 abc"valid.
If you place the cursor between the characters b and c and press the return button, I need to delete it b, the result will be "12 ac". Instead, he removes it and adds a space, becoming the following: "12 a c".
A simple code example is shown below.
I would appreciate any thoughts or examples to get around this issue.
public class testFrame extends javax.swing.JFrame {
public testFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
setMinimumSize(new Dimension(300,150));
java.awt.Button closeButton = new java.awt.Button();
JFormattedTextField maskTextField = new JFormattedTextField();
maskTextField.setMinimumSize(new Dimension(100,30));
getContentPane().add(maskTextField);
closeButton.setLabel("close");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
});
getContentPane().add(closeButton);
try {
MaskFormatter someMask = new MaskFormatter("*#****");
DefaultFormatterFactory formatterFactory
= new DefaultFormatterFactory(someMask);
maskTextField.setFormatterFactory(formatterFactory);
} catch (ParseException ex) {
ex.printStackTrace();
}
maskTextField.setText("12 abc");
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testFrame().setVisible(true);
}
});
}
}
. , . , , .
testFrame javax.swing.JFrame {
public testFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(300, 200));
getContentPane().setLayout(new java.awt.FlowLayout());
JFormattedTextField maskTextField = new JFormattedTextField();
maskTextField.setMinimumSize(new Dimension(100,30));
getContentPane().add(maskTextField);
JFormattedTextField maskTextField2 = new JFormattedTextField();
maskTextField2.setMinimumSize(new Dimension(100,30));
getContentPane().add(maskTextField2);
java.awt.Button closeButton = new java.awt.Button();
closeButton.setLabel("close");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
});
getContentPane().add(closeButton);
try {
MaskFormatter someMask = new MaskFormatter("*#****");
DefaultFormatterFactory formatterFactory =
new DefaultFormatterFactory(someMask);
maskTextField.setFormatterFactory(formatterFactory);
MaskFormatter someMask2 = new MaskFormatter("*#****");
DefaultFormatterFactory formatterFactory2 =
new DefaultFormatterFactory(someMask2);
maskTextField2.setFormatterFactory(formatterFactory2);
} catch (ParseException ex) {
ex.printStackTrace();
}
maskTextField.setText("12 abc");
maskTextField2.setText("12 abc");
if (maskTextField.getFormatter() instanceof DefaultFormatter) {
DefaultFormatter f = (DefaultFormatter) maskTextField.getFormatter();
f.setAllowsInvalid(true);
maskTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);
}
pack();
this.setLocationRelativeTo(null);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testFrame().setVisible(true);
}
});
}
}