2016-08-27 83 views
1

我有一个由两个RadioButton控制的JFormattedTextField。在其中一个RadioButton中,我设置了掩码,另一个我想清除掩码并正常输入。设置为正常输入后,它不会返回getText()的值,该值仅在设置掩码后才会返回。如何删除JFormattedTextField的蒙版?

如何解决这个问题?

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(); 
    } 
} 
+2

你叫'commitEdit的()'您JFormattedTextField上调用'的getValue()'之前?如果没有,你会想要这样做。 –

回答

4

确保调用的getValue之前调用您的JFormattedTextField commitEdit()()。根据JFormattedTextField API部分getValue()

返回上一个有效值。基于AbstractFormatter的编辑策略,这可能不会返回当前值。当前编辑的值可以通过调用commitEdit和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()); 
    } 
} 

在未来,请考虑采取一些时间来创建和发布minimal example programSSCCE,因为这将让人们以最好和最快的方法充分理解你的问题,然后能够帮助你。请参阅我的代码作为一个例子。

0

上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); 
} 

}