I have a JTable and need to change some of it's properties, such as it's Highlight colour.
This website indicates there are fields in the table such as "highlight" and "light".

I can't find them anywhere. When you create an instance of a JTable, nothing involving "Highlight" or "light" show up in intellisense. (However some methods such as table.setFont() do show up).
I did however notice table.setUI(); but I don't know how to use it, or if it has anything to do with this.
What I exactly am looking for is to make a row change it's colour when you hover the mouse over it, I'm not sure if Highlight will do this or if I will have to go the long way by implementing the mouseListener. I still want to be able to change other L&F settings though.
I'm not sure the codes below will work because I grabbed and modified it from some chunks of one of my old project's codes, but you could try.
public class MyTable extends JTable implements MouseMotionListener {
int mouseHoverRow = -1;
public MyTable() {
addMouseMotionListener(this);
}
//codes
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int vColIndex) {
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
Color back = getSelectionBackground();
int red = back.getRed();
int green = back.getGreen();
int blue = back.getBlue();
if (rowIndex == mouseHoverRow) {
c.setBackground(Color.LIGHT_GRAY);
} else {
c.setBackground(getBackground());
}
return c;
}
public void mouseMoved(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
mouseHoverRow = rowAtPoint(p);
}
You can probably reach your goal by setting one or several properties you cite in the UIManager (or the UIDefaults) using the method #put(String, String) before rendering your JTable.
This link may also help you a bit: http://www.java2s.com/Code/Java/Swing-JFC/ListUIPropertiesinaJTableandsortable.htm
Hope it will help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With