如何解决JTable 列在某个列号后停止工作
我的印象是,在第 4 列 [索引 3] 之后,我的 jtable 的其余部分确实会对格式设置或数据验证做出反应。
正如您在此图像中看到的,颜色在第 4 列之后停止:
声明:
private final JTable table = new JTable(new TableModelDB("all"));
private final JScrollPane scrollPane = new JScrollPane(table);
这是我所有与 jtable 相关的代码:
jformattedtextfield ftext = new jformattedtextfield();
try {
MaskFormatter mask = new MaskFormatter("####-##-##");
mask.setPlaceholderCharacter('_');
mask.setAllowsInvalid(false);
mask.install(ftext);
} catch (ParseException e) {
System.out.println("error: " + e);
}
table.setDefaultEditor(Object.class,new MyCellEditor());
table.getColumnModel().getColumn(3).setCellEditor(new MyCellEditor(ftext));
table.setRowHeight(30);
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(this);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultRenderer(Object.class,cr);
table.setautocreateColumnsFromModel(false);
resizeColumnWidth();
table.getTableHeader().setFont(new Font("Arial",Font.BOLD,20));
table.setFont(new Font("Arial",Font.PLAIN,20));
使用 CellEditor 进行数据验证(验证工作正常,直到 column=4,使用 println() 我发现从来没有调用过 stopCellEditing 方法):
private boolean editValidation(String data) {
System.out.println("editValidation");
int column = table.getSelectedColumn();
if (data == null || data.trim().isEmpty()) {
System.out.println("c0");
JOptionPane.showMessageDialog(null,"Cannot leave a field blank");
return false;
}
if (column == 1 && data.length() > 255) {
JOptionPane.showMessageDialog(null,"The Income name is too long (Max 255 characters)");
return false;
}
if (column == 2 && data.length() > 255) {
JOptionPane.showMessageDialog(null,"The Category name is too long (Max 255 characters)");
return false;
}
if (column == 3) {
if (data.contains("_")) {
JOptionPane.showMessageDialog(null,"Invalid date");
return false;
} else {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
df.parse(data);
} catch (ParseException e) {
JOptionPane.showMessageDialog(null,"Invalid date");
return false;
}
}
}
if (column == 4) {
System.out.println("c4");
if (!data.matches("^[0-9]+")) {
JOptionPane.showMessageDialog(null,"Amount contains non-number elements");
return false;
} else if (Integer.parseInt(data) <= 0) {
JOptionPane.showMessageDialog(null,"Income cannot be less than or equal to zero");
return false;
}
}
return true;
}
public class MyCellEditor extends DefaultCellEditor {
public MyCellEditor() {
super(new JTextField());
}
MyCellEditor(jformattedtextfield ftext) {
super(ftext);
}
@Override
public boolean stopCellEditing() {
System.out.println("stopCellEditing");
final JTextField field = (JTextField) getComponent();
if (editValidation(field.getText())) {
System.out.println("case t");
field.setBackground(Color.WHITE);
return super.stopCellEditing();
}
System.out.println("case f");
Toolkit.getDefaultToolkit().beep();
field.setBackground(Color.RED);
return false;
}
}
Cell Renderer(即使我将所有内容都设置为浅灰色,索引 3 之后的列并未应用):
public class CustomCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col) {
Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
c.setBackground(Color.LIGHT_GRAY);
// if (col == 0) {
// c.setBackground(Color.LIGHT_GRAY);
// } else {
// c.setBackground(Color.WHITE);
// }
return c;
}
}
表格模型:
public final class TableModelDB extends AbstractTableModel {
private final WorkingClass wc = new WorkingClass();
private final int startRows = 1;
private final String[] columnNames = {"Income ID","Income Name","Category","Entry Date","Amount","Delete"};
private Object[][] data;
public TableModelDB(String type) {
try {
ResultSet rs = wc.searchIncomeRS(type);
int rowCount = wc.rowCount();
int count = 0,amount,total = 0;
Object[][] temp = new Object[rowCount + startRows][6];
while (rs.next()) {
temp[count][0] = rs.getString("ID");
temp[count][1] = rs.getString("EntryName");
temp[count][2] = rs.getString("Category");
Date date = new Date(rs.getDate("EntryDate").getTime());
temp[count][3] = wc.strDate(date);
amount = rs.getInt("Amount");
total += amount;
temp[count][4] = amount;
temp[count++][5] = false;
}
temp[count][4] = total;
data = temp;
} catch (sqlException ex) {
System.out.println("error: " + ex);
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row,int col) {
return data[row][col];
}
public Class getColumnClass(int col) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row,col);
if (o != null) {
return o.getClass();
}
}
return Object.class;
}
public boolean isCellEditable(int row,int col) {
return !(data[row][col] == null || data[row][0] == null || col == 0);
}
public void setValueAt(Object value,int col) {
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
如果哪位好心人能告诉我为什么专栏没有回应将不胜感激,也欢迎任何其他错误或批评
非常感谢:)
解决方法
在创建了几个测试类之后,我意识到只有带有字符串的列才能获得格式和 DV,因此我将其更改为:
table.setDefaultEditor(String.class,new MyCellEditor());//DV
table.setDefaultEditor(Integer.class,new MyCellEditor());//DV
table.setDefaultEditor(Boolean.class,new MyCellEditor());//DV
table.getColumnModel().getColumn(3).setCellEditor(new MyCellEditor(ftext));//Mask
table.setDefaultRenderer(String.class,new CustomCellRenderer());//color
table.setDefaultRenderer(Integer.class,new CustomCellRenderer());//color
table.setDefaultRenderer(Boolean.class,new CustomCellRenderer());//color
现在一切正常
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。