我想知道以下是否可行以及是否如何做到这一点。我想删除或隐藏或禁用选择 我桌上的“空”单元格:
以下是设置表模型的代码,在此代码之后我只用数据填充表:
myTable.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String[]{
null, null, null
}) {
Class[] types = new Class[]{
java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean[]{
false, false, false
};
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
});
答案 0 :(得分:2)
好吧,经过一段时间的黑客攻击后,我想我有一个可能的解决方案。
table.setCellSelectionEnabled(true);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setDefaultRenderer(Object.class, new Renderer());
public class Renderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (table.getValueAt(row, column) == null && isSelected) {
table.clearSelection();
return super.getTableCellRendererComponent(table, value, false, false,
row, column);
} else {
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}
}
这个原因只有在你有
的情况下才有效table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
启用。并且空单元格仍然具有焦点。但它可能足以满足您的要求