vedran
vedran

Reputation: 2188

how can I change the background color of a cell at a given row and index?

I can't believe I can't find while googling. Every JTable I programmed was automatically filled with data from DB and any editing was done through listeners, but now for testing purposes I just have a simple 3x3 table and I have no idea how to manually select a cell in code (in order to change bg color for testing purposes), something like table.cellAt(1,1).setBGcolor...

EDIT: Since title was not formed as a question, How do I manually select a cell in my code?

Upvotes: 0

Views: 1692

Answers (2)

JB Nizet
JB Nizet

Reputation: 692081

So your question is rather "how can I change the background color of a cell at a given row and index?".

The background color of a cell depends on the renderer associated to the cell. But you can't associate a renderer to a cell. What you can do is

  • create a JTable subclass and override getCellRenderer(int row, int column) to return your own renderer
  • call setDefaultRenderer(Class columnClass, TableCellRenderer renderer) to associate a renderer to a given class of data

So, if you want to change the background color of a given cell, you must configure your table to use a custom renderer, and you must configure this custom renderer to use your background color instead of the default one for this given cell (or change the value of the data in this particular cell so that your custom renderer knows it must use the background color).

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

How do I manually select a cell in my code?

table.changeSelection(row, column, false, false), for example

Upvotes: 9

Related Questions