I am having trouble with masks in a JFormattedTextField
I understand that it replaces invalid characters with a space, or whatever you define via setPlaceholderCharacter, but what I need it to do is allow deletion or backspace, and NOT insert a space in place of the character I deleted as long as the rest of the string is allowed in the mask.
For example, with the mask: *#*****
, the string "12 abc"
is valid.
If you put your cursor between the b and c characters, and press the backspace button, I need it to delete the b, resulting in "12 ac"
. Instead, it deletes it, and adds a space, becoming: "12 a c"
.
A simple code example is below to demonstrate.
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);
}
});
}
}
Updating code to reflect answer below. I added a second field so you can see the behaviour with and without the fix. Also a minor fix, I resized the windows and centred it in the screen to make it more friendly.
public class testFrame extends 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");
// added per suggestion below
if (maskTextField.getFormatter() instanceof DefaultFormatter) {
DefaultFormatter f = (DefaultFormatter) maskTextField.getFormatter();
f.setAllowsInvalid(true);
// options are:
// JFormattedTextField.COMMIT
// JFormattedTextField.COMMIT_OR_REVERT --> default
// JFormattedTextField.REVERT
// JFormattedTextField.PERSIST
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);
}
});
}
}