如何动态更改FormatedTextField掩码?

时间:2015-05-23 20:27:57

标签: java swing

所以我有这个FormatedTextField

JFormattedTextField myFtf = new JFormattedTextField();

具有以下掩码,放在我的应用程序构造函数

try {
    myFtf.setFormatterFactory(
        new DefaultFormatterFactory(
           new MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
    ex.printStackTrace();
}

然后,我有两个单选按钮,应该在myFtf中更改掩码格式化程序。

我尝试了以下内容:

private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
        myFtf.setFormatterFactory(
            new DefaultFormatterFactory(
                new MaskFormatter("###.###.###-##")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}                                                 

private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                      
    try {
        myFtf.setFormatterFactory(
            new DefaultFormatterFactory(
               new MaskFormatter("##.###.###/####-##")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

哪个工作正常,直到我在文本字段中有输入时尝试更改其掩码。如果有,它不再改变面具。这是一些印刷品:

确定方案:

img a: enter image description here

切换单选按钮给了我这个:

img b: enter image description here

越野车场景:

img c: enter image description here

切换单选按钮给了我这个:

img d: enter image description here

我希望 img d img a

完全相同

如何正确动态更改其遮罩?

2 个答案:

答案 0 :(得分:0)

将您的动作侦听器更改为:

private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    try {
        myFtf.setFormatterFactory(
            new DefaultFormatterFactory(
                new MaskFormatter("###.###.###-##")));
        myFtf.setText("");
    } catch (Exception e) {
        e.printStackTrace();
    }
}                                                 

private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                      
    try {
        myFtf.setFormatterFactory(
            new DefaultFormatterFactory(
               new MaskFormatter("##.###.###/####-##")));
        myFtf.setText("");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

那应该清除文本字段。 祝你好运!

答案 1 :(得分:0)

我让它正常工作!我需要做的就是添加一个

myFtf.setText("")

设置新格式化工厂后。 ; first of all, set the video mode mov ah, 0x00 ; function: set video mode mov al, 0x03 ; video mode: 0x03 int 0x10 ; set the cursor position to 0:0 (DH:DL) mov ah, 0x02 ; function: set cursor position xor bh, bh ; page: 0x00 xor dh, dh ; row: 0x00 xor dl, dl ; column: 0x00 int 0x10 ; read row into DH call read_num mov dh, dl ; if you want to, you can read a separator character between them ; call read_num ; read column into DL call read_num ; set the cursor position again to DH:DL mov ah, 0x02 ; function: set cursor position xor bh, bh ; bh: just to make sure BH remains 0x00 int 0x10 ... read_num: ; will read two decimal characters from input to DL ; DOS interrupt for reading a single characted mov ah, 0x01 ; function: read a character from STDIN int 0x21 and al, 0x0F ; get the number from the ASCII code mov bl, 0x0A mul bl ; move it to the place of tens mov dl, al ; dl = al*10 ; DOS interrupt for reading another char mov ah, 0x01 ; function: read a character from STDIN int 0x21 and al, 0x0F add dl, al ; dl = dl + al ret 没有按预期工作,但这是一个近景! :-)