简体   繁体   English

JTable 删除或隐藏或禁用特定单元格?

[英]JTable remove or hide or disable a particular cell?

I am wondering if the following is possible and if it is how to do it.我想知道以下是否可行,以及如何去做。 I would like to remove or hide or disable selection of the "empty" cell from my table:我想从我的表格中删除或隐藏或禁用“空”单元格的选择:

在此处输入图片说明

The following is the code that sets the table model, after this code I just populate the table with data:以下是设置表格模型的代码,在这段代码之后我只是用数据填充表格:

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];
    }
}); 

okay after a bit of hacking i think i have a possible solution for you.好吧,经过一些黑客攻击,我想我有一个可能的解决方案。

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);
    }
}

} }

this of cause only works if you have这个原因只有在你有时才有效

table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

enabled.启用。 and the empty cell still has the focus.并且空单元格仍然具有焦点。 but it may be good enough for your requirements但它可能足以满足您的要求

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

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