繁体   English   中英

将MaskFormatter应用于我的JTable中的列,但是该掩码仅在我在该列中编辑的第一个单元格上使用

[英]Applying a MaskFormatter to a column in my JTable, but the mask is only used on the first cell I edit in the column

////DOB column formats to dd/mm/yy
    TableColumn dobColumn = table.getColumnModel().getColumn(3);
    DateFormat df = new SimpleDateFormat("dd/mm/yy");
    JFormattedTextField tf = new JFormattedTextField(df);
    tf.setColumns(8);
    try {
        MaskFormatter dobMask = new MaskFormatter("##/##/##");
        dobMask.setPlaceholderCharacter('0');
        dobMask.install(tf);
    } catch (ParseException ex) {
        Logger.getLogger(DisplayStudents.class.getName()).log(Level.SEVERE, null, ex);
    }
    dobColumn.setCellEditor(new DefaultCellEditor(tf));

我已经按照类似的过程将列中的单元格转换为ComboBoxes或CheckBoxes,并且这些列中的所有单元格都已设置为ComoboBoxes / CheckBoxes,但是当我将DOB列的单元格编辑器设置为带有掩码的JFormattedTextField时,遮罩仅应用于我在该列中单击的第一个单元格。

编辑:这是我的SSCCE:

public class TableExample extends JFrame {
    public TableExample() {
    add(makeTable());
}

private JTable makeTable() {
    Object[][] tableData = {{"","a","b",""}, {"","c","d",""}};
    String[] columns = {"comboBox column", "column2", "column3", "dobColumn"};
    JTable table = new JTable(tableData, columns);
    ////turn into a combo box
    TableColumn comboColumn = table.getColumnModel().getColumn(0);
    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addItem("1st");comboBox.addItem("2nd");
    comboColumn.setCellEditor(new DefaultCellEditor(comboBox));
    ////DOB column formats to dd/mm/yy
    TableColumn dobColumn = table.getColumnModel().getColumn(3);
    DateFormat df = new SimpleDateFormat("dd/mm/yy");
    JFormattedTextField tf = new JFormattedTextField(df);
    tf.setColumns(8);
    try {
        MaskFormatter dobMask = new MaskFormatter("##/##/##");
        dobMask.setPlaceholderCharacter('0');
        dobMask.install(tf);
        } catch (ParseException ex) {
     Logger.getLogger(TableExample.class.getName()).log(Level.SEVERE, null, ex);
    }
    dobColumn.setCellEditor(new DefaultCellEditor(tf));

    return table;
}

public static void main(String[] args) {
    JFrame frame = new TableExample();
    frame.setSize( 300, 300 );
    frame.setVisible(true); 
}

}

我仍然不确定为什么每次单击dobColumn内的单元格都会破坏Mask。 因此,我决定实现tableChange方法,以便在dobColumn中发生更改时重新创建掩码。

public void tableChanged(TableEvent e) {
    if(e.getColumn() == 3) {    //if column edited was the dobColumn
        System.out.println("Remaking mask");
        JFormattedTextField tf = new JFormattedTextField();
        try {
             MaskFormatter dobMask = new MaskFormatter("##-##-##");
             dobMask.setPlaceholderCharacter('0');
             dobMask.install(tf);
        } catch (ParseException ex) {
            Logger.getLogger(DisplayStudents.class.getName()).log(Level.SEVERE, null, ex);
        }

        table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(tf));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM