1

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:

enter image description here

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];
    }
}); 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
jadrijan
  • 1,438
  • 4
  • 31
  • 48
  • could you explain a bit more what you mean with hiding the cell? do you want the selection to be unchanged if you click the empty cell? do you want to change the default rendering of a cell if its empty? – schippi Nov 29 '12 at 16:57
  • this was solved a few times here about selection, by moving selection to another cell, sorry I'm never used, nor remember how ... – mKorbel Nov 29 '12 at 16:57
  • this empty cell, I just want to hide it or anything that is the best in your opinion so that the user cannot select it but can select any non empty cell. – jadrijan Nov 29 '12 at 17:05

1 Answers1

2

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

schippi
  • 1,034
  • 5
  • 15
  • i tried a couple of things, but i couldnt get the right column in the ListSelectionListener. also i couldnt remove the default ListSelectionListener from the Model so i couldnt garuantee that the deselectListener would trigger after the Listener who is responsible to select it. – schippi Nov 29 '12 at 19:25
  • Sorry, but that doesn't make sense. TableModels don't have selection listeners. Replacing the tables selection listener will update any attached listeners. The question is, how to determine which cell s selectable – MadProgrammer Nov 29 '12 at 20:04
  • [Here's](http://stackoverflow.com/questions/7936064/before-cell-select-jtable-event) at least one example – MadProgrammer Nov 29 '12 at 20:13