Java 如何删除JFormattedTextField的掩码?
我有一个由两个单选按钮控制的JFormattedTextField。在其中一个RadioButton中,我设置了掩码,另一个我希望清除掩码并正常键入。设置为“正常类型”后,它不会返回getText的值,该值仅在设置掩码时返回 如何解决这个问题Java 如何删除JFormattedTextField的掩码?,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,我有一个由两个单选按钮控制的JFormattedTextField。在其中一个RadioButton中,我设置了掩码,另一个我希望清除掩码并正常键入。设置为“正常类型”后,它不会返回getText的值,该值仅在设置掩码时返回 如何解决这个问题 private void setMask() { MaskFormatter formatter = null; try { txtPesquisar.setValue(null); if (rbNome.i
private void setMask() {
MaskFormatter formatter = null;
try {
txtPesquisar.setValue(null);
if (rbNome.isSelected()) {
//clear mask to type normally
formatter = new MaskFormatter("****************************************");
formatter.setPlaceholderCharacter(' ');
} else {
//set mask
formatter = new MaskFormatter("###.###.###-##");
formatter.setPlaceholderCharacter(' ');
}
txtPesquisar.setFormatterFactory(new DefaultFormatterFactory(formatter));
txtPesquisar.requestFocus();
txtPesquisar.selectAll();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
在调用getValue之前,请确保在JFormattedTextField上调用CommittedIt。根据getValue部分:
返回最后一个有效值。根据AbstractFormatter的编辑策略,这可能不会返回当前值。当前编辑的值可以通过调用CommittedIt然后调用getValue来获得。
返回:
例如:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
@SuppressWarnings("serial")
public class TestFormattedField extends JPanel {
private JFormattedTextField txtPesquisar = new JFormattedTextField();
private JRadioButton rbNome = new JRadioButton("None");
private JRadioButton rbFormat = new JRadioButton("Format");
public TestFormattedField() {
txtPesquisar.setColumns(20);
ButtonGroup btnGroup = new ButtonGroup();
btnGroup.add(rbFormat);
btnGroup.add(rbNome);
rbNome.setSelected(true);
rbNome.setMnemonic(KeyEvent.VK_N);
rbFormat.setMnemonic(KeyEvent.VK_F);
add(txtPesquisar);
add(rbFormat);
add(rbNome);
setMask();
add(new JButton(new SetFormatAction()));
add(new JButton(new GetTextAction()));
}
private void setMask() {
MaskFormatter formatter = null;
try {
txtPesquisar.setValue(null);
if (rbNome.isSelected()) {
//clear mask to type normally
formatter = new MaskFormatter("****************************************");
formatter.setPlaceholderCharacter(' ');
} else {
//set mask
formatter = new MaskFormatter("###.###.###-##");
formatter.setPlaceholderCharacter(' ');
}
txtPesquisar.setFormatterFactory(new DefaultFormatterFactory(formatter));
txtPesquisar.requestFocus();
txtPesquisar.selectAll();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
private class SetFormatAction extends AbstractAction {
public SetFormatAction() {
super("Set Format");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
@Override
public void actionPerformed(ActionEvent e) {
setMask();
}
}
private class GetTextAction extends AbstractAction {
public GetTextAction() {
super("Get Text");
putValue(MNEMONIC_KEY, KeyEvent.VK_G);
}
@Override
public void actionPerformed(ActionEvent e) {
final String text = txtPesquisar.getText();
try {
txtPesquisar.commitEdit();
} catch (ParseException e1) {
String title = "Incomplete Text Entry";
String msg = "Text -- " + text + " is not yet complete";
JOptionPane.showMessageDialog(TestFormattedField.this, msg, title, JOptionPane.ERROR_MESSAGE);
}
Object value = txtPesquisar.getValue();
System.out.println("text: " + text);
System.out.println("value: " + value);
}
}
private static void createAndShowGui() {
TestFormattedField mainPanel = new TestFormattedField();
JFrame frame = new JFrame("Test JFormattedField");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
在未来,请考虑花些时间来创建和张贴一个或因为这将是最好的和最快的方式,让人们充分了解你的问题,然后能够帮助你。请以我的代码为例。在调用getValue之前,请确保在JFormattedTextField上调用CommittedIt。根据getValue部分:
返回最后一个有效值。根据AbstractFormatter的编辑策略,这可能不会返回当前值。当前编辑的值可以通过调用CommittedIt然后调用getValue来获得。
返回:
例如:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
@SuppressWarnings("serial")
public class TestFormattedField extends JPanel {
private JFormattedTextField txtPesquisar = new JFormattedTextField();
private JRadioButton rbNome = new JRadioButton("None");
private JRadioButton rbFormat = new JRadioButton("Format");
public TestFormattedField() {
txtPesquisar.setColumns(20);
ButtonGroup btnGroup = new ButtonGroup();
btnGroup.add(rbFormat);
btnGroup.add(rbNome);
rbNome.setSelected(true);
rbNome.setMnemonic(KeyEvent.VK_N);
rbFormat.setMnemonic(KeyEvent.VK_F);
add(txtPesquisar);
add(rbFormat);
add(rbNome);
setMask();
add(new JButton(new SetFormatAction()));
add(new JButton(new GetTextAction()));
}
private void setMask() {
MaskFormatter formatter = null;
try {
txtPesquisar.setValue(null);
if (rbNome.isSelected()) {
//clear mask to type normally
formatter = new MaskFormatter("****************************************");
formatter.setPlaceholderCharacter(' ');
} else {
//set mask
formatter = new MaskFormatter("###.###.###-##");
formatter.setPlaceholderCharacter(' ');
}
txtPesquisar.setFormatterFactory(new DefaultFormatterFactory(formatter));
txtPesquisar.requestFocus();
txtPesquisar.selectAll();
} catch (ParseException ex) {
ex.printStackTrace();
}
}
private class SetFormatAction extends AbstractAction {
public SetFormatAction() {
super("Set Format");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
@Override
public void actionPerformed(ActionEvent e) {
setMask();
}
}
private class GetTextAction extends AbstractAction {
public GetTextAction() {
super("Get Text");
putValue(MNEMONIC_KEY, KeyEvent.VK_G);
}
@Override
public void actionPerformed(ActionEvent e) {
final String text = txtPesquisar.getText();
try {
txtPesquisar.commitEdit();
} catch (ParseException e1) {
String title = "Incomplete Text Entry";
String msg = "Text -- " + text + " is not yet complete";
JOptionPane.showMessageDialog(TestFormattedField.this, msg, title, JOptionPane.ERROR_MESSAGE);
}
Object value = txtPesquisar.getValue();
System.out.println("text: " + text);
System.out.println("value: " + value);
}
}
private static void createAndShowGui() {
TestFormattedField mainPanel = new TestFormattedField();
JFrame frame = new JFrame("Test JFormattedField");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
在未来,请考虑花些时间来创建和张贴一个或因为这将是最好的和最快的方式,让人们充分了解你的问题,然后能够帮助你。请以我的代码为例。关于inintcomponets之后的表单构造函数
public Form1() {
initComponents();
MaskFormatter dateMask;
try {
dateMask = new MaskFormatter("|#|#|#|#|#|#|#|#|#|#|");
dateMask.install(JTEXTFORMATEE);
} catch (ParseException ex) {
Logger.getLogger(Forma051.class.getName()).log(Level.SEVERE, null, ex);
}
}
关于inintcomponets之后的表单构造函数
public Form1() {
initComponents();
MaskFormatter dateMask;
try {
dateMask = new MaskFormatter("|#|#|#|#|#|#|#|#|#|#|");
dateMask.install(JTEXTFORMATEE);
} catch (ParseException ex) {
Logger.getLogger(Forma051.class.getName()).log(Level.SEVERE, null, ex);
}
}
在调用getValue之前,是否在JFormattedTextField上调用CommittedIt?如果没有,则需要执行此操作。在调用getValue之前,是否在JFormattedTextField上调用CommittedIt?如果没有,您将希望这样做。