java:何时将此方法称为:getTableCellRendererComponent? [英] java : when is this method called : getTableCellRendererComponent?

查看:74
本文介绍了java:何时将此方法称为:getTableCellRendererComponent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了设置特定表格单元格的颜色,我应该创建一个具有以下方法的自定义TableCellRenderer:getTableCellRendererComponent.

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

何时将此方法称为:getTableCellRendererComponent?

仅当第一次绘制JTable时是什么?

just when a JTable is drawn for the first time ?

如果我在jframe上有一组按钮和一个jtable,并且每个按钮在按下时都会导致表中一定数量的单元格变色,那么如何在以下情况下调用此方法"getTableCellRendererComponent"我按下按钮了吗?

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 ?

推荐答案

表首次呈现时会触发此问题,但是当

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.

可以在 Example Depot <中找到使用自定义单元格渲染器的示例. /a>.进入单元格视图并进行模型更新时,将调用getTableCellRendererComponent.

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.

附录:我刚刚注意到您问题的另一部分,何时调用getTableCellRendererComponent?对于必须呈现的表中的每个可见单元格都将调用它.可以这样考虑,如果您在内部使用JLabel作为渲染器,则将返回配置为仅显示该单元格的JLabel.它将在下一个单元格上重新使用以进行渲染.想到它的最简单方法是使您的单元格看起来像一个可视化模板,您可以在getTableCellRendererComponent调用上对其进行配置,然后将其返回.调用框架将其作为图像"(缺少更好的术语)呈现给屏幕缓冲区,然后为下一个单元重新配置并再次呈现.

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.

这篇关于java:何时将此方法称为:getTableCellRendererComponent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆