4

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".

enter image description here

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
David
  • 15,652
  • 26
  • 115
  • 156
  • See also [Customizing JTable](http://stackoverflow.com/questions/6209849/customizing-jtable), a possible duplicate of your question. – trashgod Aug 12 '11 at 08:37
  • @trashgod I'm not interested in changing the header UI, but the actual fields ui, and would like some examples or help on how to do it. – David Aug 12 '11 at 08:39
  • @StanislavL Could you explain that in a better way and in a proper answer please? Ideally including an example? – David Aug 12 '11 at 08:46
  • The property strings you cite are keys used to access a map of defaults established by the `UIManager` for a particular Look & Feel. – trashgod Aug 12 '11 at 08:48
  • Renderer examples may be found [here](http://download.oracle.com/javase/tutorial/uiswing/components/table.html). – trashgod Aug 12 '11 at 08:50

3 Answers3

2

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);
    }
  • Added `table.repaint();` at the end of `mouseMoved()` and made some more modifications to this code and it worked beautifully. Thank you. – David Aug 12 '11 at 10:46
1

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!

aymeric
  • 3,877
  • 2
  • 28
  • 42
  • Thanks for the answer! I tried UIManager.put("highlight", Color.red); but it didn't seem to work though. I'll try some other colour values next. – David Aug 12 '11 at 09:14
1

there are four ways

1) override UI

2) use Renderers

3) for better and nice output to the GUI I recomending to change Look and Feel or use Nimbus Look and Feel or Synth Look and Feel

4) put all a.m. options together and with Custom painting for JViewPort, ScrollBar

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @David confortable would be look for `prepareRenderer` (works for whole TableView) as for `TableCellRenderer` (valid for particular TableColumn), please carrefully with Opacities for JTable, TableCellRenderer and TableCellEditor (for getTableCellRendererComponent(...) from link that you posted), I'm not sure that Opacity works as I expected, and that's please follows examples from this forum, there are lots of better code and excellent described – mKorbel Aug 12 '11 at 10:07
  • +1 especially for 3) and the good links. It was hard to choose an answer, but I was looking for an SSCCE. I have learnt alot from this though. – David Aug 12 '11 at 10:50