0

I'm trying to figure out why some JTables in a large application have the focus indicator and some don't. To debug this issue, I added code:

UIManager.put("Table.focusCellHighlightBorder",new BorderUIResource(
    new LineBorder(new Color(255,0,0))); 

And those JTables with focus indicators changed to red but I still don't see the focus indicator on all JTables. Any idea why the cells in a JTable wouldn't show the focus indicator?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
splatek
  • 111
  • 1
  • 3
  • 1
    Have you checked for `null`? See also this [related, previous question](http://stackoverflow.com/questions/6877603/change-the-color-of-the-java-swing-component-focus-indicator/6877841#6877841). – trashgod Jul 29 '11 at 21:10
  • @trashgod please see my post, based on camickr's example – mKorbel Jul 29 '11 at 21:58
  • @trashgod I checked border in the cellRenderer in both a JTable with the focus indicator and one without a focus indicator and both have border of right=left=top=bottom=1; I'm still looking for what's different between these two JTables but no luck so far. – splatek Jul 29 '11 at 22:07
  • @splatek, you still haven't posted a SSCCE!! – camickr Jul 30 '11 at 03:25

3 Answers3

1

You need to set the UI property "before" creating the table.

If you still have a problem then post your SSCCE that demonstrates the problem becuase we can't guess what you are doing.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm not trying to be difficult, its just that I have very little Java experience and if I try to isolate this code (pulling pieces of the code from several classes) and create a SSCCE there is no guarantee it will have the same bug. I was more interested in knowing what attributes/methods control the displaying of the focus indicator so that I might know where to concentrate my effort in searching for this bug. Perhaps I just need to struggle on my own for a bit longer. If I do succeed in getting a SSCCE or a solution, I'll be sure to post it. – splatek Aug 01 '11 at 14:18
  • That is the whole point of creating a SSCCE to simplify the problem. Generally when you create a SSCCE you find that the code works. So you then need to determine what is different with the working code and your real code. You do that by adding back in features one at a time. Then when it stops working you have isolated the problem. Now you can either fix it or if you don't understand why it causes the problem you can ask a question with more specific information. – camickr Aug 01 '11 at 14:53
  • Thanks for the hint about SSCCE. I'll try to use it for my next question. – splatek Aug 01 '11 at 15:53
1

maybe s/he means (add to your example something ...)

public JavaGUI() {
    CustomModel model = new CustomModel();
    JTable table = new JTable(model) {

        private static final long serialVersionUID = 1L;
        private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
        private Border inside = new EmptyBorder(0, 1, 0, 1);
        private Border highlight = new CompoundBorder(outside, inside);

        @Override
        public Component prepareRenderer(
                TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);
            JComponent jc = (JComponent) c;
            if (isRowSelected(row)) {
                jc.setBackground(Color.orange);
                jc.setBorder(highlight);
            } else {
                jc.setBackground(Color.white);
            }
            return c;
        }
    };
    for (int i = 1; i <= 16; i++) {
        model.addRow(newRow(i));
    }
    this.add(table);
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Ah, I just added the poster's `UIManager.put()` to the beginning of my [example's](http://stackoverflow.com/questions/6873665/jtable-row-selection-background-problem/6874437#6874437) `display()` method, as @camickr also suggests. – trashgod Jul 29 '11 at 22:08
0

Both JTables used a cell renderer that subclassed DefaultTableCellRenderer and overrode the getTableCellRendererComponent method. The overridden getTableCellRendererComponent method for the JTable that showed the focus indicator, called the super.getTableCellRendererComponent method but the overridden getTableCellRendererComponent method for the JTable that did not show the focus indicators did NOT call the super.getTableCellRendererComponent method.

JTable with focus indicator:

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int col) {
    Component comp = super.getTableCellRendererComponent(table, value,
        isSelected, hasFocus, row, col);
    ....

JTable with no focus indicator:

    public Component getTableCellRendererComponent(JTable table, Object value,
                                             boolean isSelected,
                                             boolean hasFocus, int row,
                                             int col) {
  for (int i = 0; i < ids.length; i++) {
      ....
splatek
  • 111
  • 1
  • 3