简体   繁体   中英

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?

How do I manually select a cell in my code?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM