[英]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));
I've followed a similar process to turn cells in a column into ComboBoxes or CheckBoxes and all cells inside those columns have been set to ComoboBoxes/CheckBoxes, but when I set the cell editor for the DOB column to a JFormattedTextField with a mask, the mask is only applied to the first cell I click on in the column. 我已经按照类似的过程将列中的单元格转换为ComboBoxes或CheckBoxes,并且这些列中的所有单元格都已设置为ComoboBoxes / CheckBoxes,但是当我将DOB列的单元格编辑器设置为带有掩码的JFormattedTextField时,遮罩仅应用于我在该列中单击的第一个单元格。
EDIT: Here's my SSCCE: 编辑:这是我的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);
}
} }
I'm still unsure as to why the Mask is destroyed everytime I click a cell inside the dobColumn. 我仍然不确定为什么每次单击dobColumn内的单元格都会破坏Mask。 So I decided to implement the tableChange method, to recreate the mask whenever a change occurs in the dobColumn 因此,我决定实现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.