java : when is this method called : getTableCellRendererComponent?

荒凉一梦 提交于 2020-01-04 17:39:09

问题


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 of cells in the table to be colored, how can I cause this method "getTableCellRendererComponent" to be called when I press on the button ?


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/6424728/java-when-is-this-method-called-gettablecellrenderercomponent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!