如何将字体颜色应用于使用DefaultTableModel的JTable上的特定单元格 [英] How to apply Font color to specific cells on JTable that uses DefaultTableModel

查看:128
本文介绍了如何将字体颜色应用于使用DefaultTableModel的JTable上的特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个连接到MS Access的简单待办事项列表Java应用程序,并使用 JTable DefaultTableModel 来显示列表。我想在单击按钮时通过更改颜色来标记已完成的任务。我有一个名为' completed '的布尔字段作为指标。

I'm trying to create a simple To-Do list Java application connected to MS Access and I used JTable and DefaultTableModel to 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.

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

我已经有一个名为 customFont 的Font对象,已准备就绪要施加。我的问题是,如何将它仅应用于已完成== true的特定单元格。

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.

示例代码将非常受欢迎。

Sample codes would be much appreciated.

推荐答案


  • 最简单的方法是查看 prepareRenderer(),最重要的是@camick 表行渲染

    • easiest of ways is look at prepareRenderer(), best of all is @camickr Table Row Rendering

      JTable 是View,基于 TableModel ,在大多数情况下,您必须将视图转换为模型 converXxxToXxx prepareRenderer getTableCellRendererComponent ,因为 JTable 可以对已过滤的

      JTable is View, based on TableModel, in most cases you have to convert the view against model converXxxToXxx from inside of prepareRenderer or getTableCellRendererComponent, because JTable could be sorted of filtered

      方法

      代码

      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
      Component c = super.prepareRenderer(renderer, row, column);
      

      返回对JTable中特定单元格的访问 - ( TableCellRenderer渲染器,int行,int列

      returns access to the specific cell in JTable - (TableCellRenderer renderer, int row, int column)

      这篇关于如何将字体颜色应用于使用DefaultTableModel的JTable上的特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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