0

I am making use of a custom JTable and AbstractTableModel, but have encountered some interesting behavior when it comes to highlighting/selecting rows.

Alright, upon startup my table looks like so, which is good:

enter image description here

But unfortunately, selecting a row gives me this:
enter image description here
This occurs in two ways:

  1. When a row in the editable "Boolean" column is clicked, there is a quick flash that looks like the above picture before the entire row is highlighted.

  2. When the row divider directly below a row is clicked in the "Boolean" column. In this case, the table stays like the above picture until another row is selected.

rrrr-o
  • 2,447
  • 2
  • 24
  • 52
Hunter S
  • 1,293
  • 1
  • 15
  • 26
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses. My "guess" would be that part of renderer customisation you are need resetting the state of your renderers appropropritly – MadProgrammer Aug 16 '15 at 23:46
  • 1
    `I am making use of a custom JTable and AbstractTableModel` - why? What is wrong with the JTable and DefaultTableModel and the default renderers of the table? I have never seen this behaviour when using the classes of the JDK. So the problem is with your custom code which we can't see. – camickr Aug 16 '15 at 23:49

1 Answers1

2

The BooleanRenderer, used internally by JTable to render TableModel values of type Boolean.class, does not exhibit this behavior. A typical implementation conditions the foreground and background colors based on the default values specified by the current look & feel. In outline, your (presumedly custom) renderer needs to do something like this:

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
        …
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        return …;
    }

A complete example is seen here.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045