java 如何将字体颜色应用于使用 DefaultTableModel 的 JTable 上的特定单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10087361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply Font color to specific cells on JTable that uses DefaultTableModel
提问by Mr. Xymon
I'm trying to create a simple To-Do list Java application connected to MS Access and I used JTableand DefaultTableModelto display the list. I want to mark completed tasks by changing its color when I click on a button. I have a boolean field named 'completed' that serves as indicator.
我正在尝试创建一个连接到 MS Access 的简单待办事项列表 Java 应用程序,我使用JTable和DefaultTableModel来显示列表。我想通过单击按钮时更改其颜色来标记已完成的任务。我有一个名为“已完成”的布尔字段作为指标。
String header[] = {"priority", "task"};
String data[][];
DefaultTableModel model = new DefaultTableModel(data, header);
JTable table = new JTable(model);
// to be replaced with code that affects only specific cells not the whole table
table.setFont(customFont);
I already have a Font object that I called customFont, which is ready to be applied. My question is, how do I apply it only to specific cells where completed==true.
我已经有一个名为customFont的 Font 对象,可以应用它了。我的问题是,如何将它仅应用于已完成==真的特定单元格。
Sample codes would be much appreciated.
示例代码将不胜感激。
回答by mKorbel
easiest of ways is look at prepareRenderer(), best of all is @camickr Table Row Rendering
JTable
is View, based onTableModel
, in most cases you have to convert the view against model converXxxToXxxfrom inside ofprepareRenderer
orgetTableCellRendererComponent
, becauseJTable
could be sorted of filteredmethods
最简单的方法是查看prepareRenderer(),最好的是@camickr Table Row Rendering
JTable
是视图,基于TableModel
,在大多数情况下,您必须从或内部针对模型converXxxToXxx转换视图,因为可以排序过滤prepareRenderer
getTableCellRendererComponent
JTable
方法
code
代码
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
returns access to the specific cell in JTable - (TableCellRenderer renderer, int row, int column)
返回对 JTable 中特定单元格的访问权限 - (TableCellRenderer 渲染器,int row, int column)
回答by pankaj_ar
Use DefaultTableCellRenderer, then you can use setForeground() and setBackground().
使用 DefaultTableCellRenderer,然后您可以使用 setForeground() 和 setBackground()。
refer to the page.. http://www.jyloo.com/news/?pubId=1282737395000
参考页面.. http://www.jyloo.com/news/?pubId=1282737395000
or see this example...
或者看这个例子...
/*This is the raw code I have written.*/
JTable Tbl=new JTable(2,2){
DefaultTableCellRenderer colortext=new DefaultTableCellRenderer();
{
colortext.setForeground(Color.RED);
}
@Override
public TableCellRenderer getCellRenderer(int arg0, int arg1) {
return colortext;
}
};
回答by Jan Hruby
I believe you can specify this behavior in
我相信你可以在
TableCellRenderer.getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column)
method of the table
表法