簡體   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