Skip to main content

JXTable setSelectedForeground highlighter colour stomps over another highlighters foreground colour

7 replies [Last post]
boomah
Offline
Joined: 2007-07-13
Points: 0

The title says it all really. If I have a highlighter with a selectedForeground colour set, that colour stomps over another highlighter's foreground colour even though the highlighter doesn't match.

The code is below:

import org.jdesktop.swingx.JXTable;

import org.jdesktop.swingx.decorator.ColorHighlighter;

import org.jdesktop.swingx.decorator.ComponentAdapter;

import org.jdesktop.swingx.decorator.HighlightPredicate;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

public class JXTableTest extends JFrame {

public JXTableTest() {

super("JXTable Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

DefaultTableModel model = new DefaultTableModel() {

@Override public int getRowCount() {return 100;}

@Override public int getColumnCount() {return 3;}

@Override public Object getValueAt(int row, int column) {return "(" + row + "," + column + ")";}

};

JXTable table = new JXTable(model);

JScrollPane scrollPane = new JScrollPane(table);

ColorHighlighter highlighter1 = new ColorHighlighter(new HighlightPredicate() {

@Override

public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {

return (adapter.row == 1);

}

});

highlighter1.setBackground(Color.LIGHT_GRAY);

highlighter1.setForeground(Color.GRAY);

highlighter1.setSelectedBackground(Color.LIGHT_GRAY.darker());

highlighter1.setSelectedForeground(Color.GRAY);

ColorHighlighter highlighter2 = new ColorHighlighter(new HighlightPredicate() {

@Override

public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {

return (adapter.row != 1);

}

});

Color color1 = new Color(190, 203, 244, 64);

highlighter2.setBackground(color1);

highlighter2.setForeground(color1.darker());

table.addHighlighter(highlighter1);

table.addHighlighter(highlighter2);

add(scrollPane);

pack();

setLocationRelativeTo(null);

setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new JXTableTest();

}

});

}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

you made my heart stomp ;-)

Not entirely sure what you mean: one possibility is covered by Karl's answer, another might be that the foreground color of the second just is the same (or appears) very similar to the selection foreground of the first. Make it something like RED to see that all okay

Thanks

Jeanette

 

 

kschaefe
Offline
Joined: 2006-06-08
Points: 0

Not setting a selection color simply means that the look and feels selection color will be used.  That's what's going on here.  If you want the selection color to be the same as the non-selection color, then you must explicitly state that.

Karl

boomah
Offline
Joined: 2007-07-13
Points: 0

It's taken a while but I've finally managed to reproduce this problem in a small test case.

It depends on various things. The table needs to be in cell selection mode. Selection has to be set in the same cell as a valid highlighter. The table header needs to be set to null. A renderer needs to be set.

I know setting a renderer isn't how we should do things in JXTable but for various reasons I have it set. Anyway, the code below shows the problem. I really hope you can tell me how to work around this problem as it is killing me.

import org.jdesktop.swingx.JXTable;

import org.jdesktop.swingx.decorator.ColorHighlighter;

import org.jdesktop.swingx.decorator.ComponentAdapter;

import org.jdesktop.swingx.decorator.HighlightPredicate;

import javax.swing.*;

import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

public class JXTableTest2 extends JFrame {

private void addHighlighters(JXTable table) {

ColorHighlighter highlighter = new ColorHighlighter(new HighlightPredicate() {

@Override

public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {

return (adapter.row == 0) && (adapter.column == 0);

}

});

highlighter.setSelectedForeground(Color.RED);

table.addHighlighter(highlighter);

}

public JXTableTest2() {

super("JXTable Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

final JPanel c = new JPanel(new BorderLayout());

DefaultTableModel model = new DefaultTableModel() {

@Override public int getRowCount() {return 100;}

@Override public int getColumnCount() {return 3;}

@Override public Object getValueAt(int row, int column) {return "(" + row + "," + column + ")";}

};

JXTable table = new JXTable(model);

table.setTableHeader(null);

table.setCellSelectionEnabled(true);

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

@Override

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

}

});

table.addRowSelectionInterval(0, 0);

table.addColumnSelectionInterval(0, 0);

addHighlighters(table);

JScrollPane scrollPane = new JScrollPane(table);

c.add(scrollPane);

add(c);

pack();

setLocationRelativeTo(null);

setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new JXTableTest2();

}

});

}

}

Sorry to stomp on your heart Jeanette.

kleopatra
Offline
Joined: 2003-06-11
Points: 0

to try and understand what's going on you might want to read the renderer related wiki entries,

http://wiki.java.net/twiki/bin/view/Javadesktop/SwingLabsSwingX#Whiteboard

Just beware: most links to the old forum threads (unfortunately, that's were the most information is buried :-( are broken.

CU
Jeanette

kleopatra
Offline
Joined: 2003-06-11
Points: 0

stomp or not: if you insist on using a misbehaving (in terms of SwingX) renderer like DefaultTableCellRenderer, you are on your own - either make it play by the rules (as defined in ComponentProvider) or suffer. There is _no_ support for that here because it's impossible.
Sorry, but those are the facts ;-)
Jeanette

boomah
Offline
Joined: 2007-07-13
Points: 0

OK, I'm not sure why I haven't done this before but you just talking about ComponentProvider reminded me about it. I just need to change my DefaultTableCellRenderer into a custom ComponentProvider, which is certainly possible. For some reason I was thinking that I would have to do everything apart from the text value with highlighters.

My problem is solved.

Thanks, Nick.

boomah
Offline
Joined: 2007-07-13
Points: 0

Hi Karl.

Thanks for the reply.

I didn't explain myself properly and looking again at the code I posted, it doesn't recreate the problem.

I still have the problem in my program though but I can't reproduce it in a small test case so I need to look into it further.

I'll get back to you.

Thanks, Nick.