开发者

java : when is this method called : getTableCellRendererComponent?

开发者 https://www.devze.com 2023-03-14 04:35 出处:网络
In order to set the color for a specific table cell, I should create a custom TableCellRenderer which has the following method : getTableCellRendererComponent.

In order to set the color for a specific table cell, I should create a custom TableCellRenderer which has the following method : getTableCellRendererComponent.

when is this method called : getTableCellRendererComponent ?

just when a JTable is drawn for the first time ?

In case I have a set of buttons and a jtable on a jframe, and each button when pressed will cause a certain number o开发者_如何学Pythonf cells in the table to be colored, how can I cause this method "getTableCellRendererComponent" to be called when I press on the button ?


This is fired when the table is first rendered but should be called on all cells when either a fireTableCellUpdated or any of the other "fire*" methods are called. I'm under the impression that you're using a class that extends AbstractTableModel (DefaultTableModel?) at which point these methods should be fired when you update a particular value of a table model.

An example of using custom cell renderers can be found at Example Depot. The getTableCellRendererComponent will be called when a cell is brought in to view and when a model update occurs.

Addendum: I just noticed one other part to your question, when is the getTableCellRendererComponent called? This is called for every visible cell in the table that must be rendered. Think of it this way, if you are using a JLabel internally as your renderer, you would return the JLabel as configured for displaying that cell only. It will be reused on the next cell for rendering it. Easiest way to think of it is a visual template that you want your cell to look like, you configure it on the getTableCellRendererComponent call, and return it. The calling framework renders it to the screen buffer as an "image" (for lack of a better term) then is reconfigured for the next cell and rendered again.


if you want to forgot for when/how/where is/are TableCell(s) updated or not then look for prepareRenderer best example as I see camickr blog

EDIT: for better/valuable hepl please edit your post and add your code


when is this method called : getTableCellRendererComponent ?

This method will get called when the UI component gets displayed/rendered for your view the very first time.

To set colors in cells of a JTable

  1. Hope you are using the custom DefaultTableCellRenderer. If so, then you can globalize the JLabel in your custom CellRenderer and then set its background color upon button click based on the row, column index.

  2. Else you can use the DefaultTableColumnModel which you have defaultrenderer/set customrenderer which will be renderered for each of the cell. Using which you can set the background color of the cell.

TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellRenderer(new MyTableCellRenderer());

Refer: Simple example to demonstrate usage of TableCellRenderer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号