JTable使用TableCellRenderer更改单元格颜色 [英] JTable change cell colors using TableCellRenderer

查看:484
本文介绍了JTable使用TableCellRenderer更改单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GUI应用程序中使用JTable作为表示游戏位置的网格。我希望表格的单元格代表某个对象的某个位置具有某种颜色,并且在某些动作上,要移动的对象(即颜色单元格在Grid / JTable中移动)。我知道我可以通过创建一个扩展 DefaultTableCellRenderer 的类来改变单元格颜色,这是唯一的方法吗?或者是否有更简单的方法来改变单元格?对于这样的应用程序,JXTable是否比JTable更好?

I am using a JTable in my GUI application as a grid to represent positions for a game. I want the cells of the table that represent a certain position of an object to have a certain color, and on some actions, the object to move (i.e the color cell to move around in the Grid /JTable). I know that I can change cell colors by making a class that extends DefaultTableCellRenderer , is this the only way it can be done? or is there a simpler way to chage cell colors??Also Is JXTable better than JTable for such an application ?

编辑:我没有'包括我需要某些单元格颜色动态更改的事实,即按钮单击,键盘点击等等,是否仍然可以使用任何 TableCellRenderer 以防我是还在使用JTable吗?

I didn't include the fact that I need certain cell colors to change dynamically, i.e with button clicks, keyboard clicks ...etc, is that still possible with any TableCellRenderer in case I am still using JTable ?

谢谢,

推荐答案

使用JTable,DefaultTableCellRenderer是最好的方法。

With JTable, DefaultTableCellRenderer is the best way to do it.

但是,如果使用JXTable,使用荧光笔和自定义谓词会更容易。通常,Predicates用于根据单元格的内容进行着色,但您可以根据行/列和一些外部值轻松地进行着色。

However, if you use JXTable, it'll be much easier to use a Highlighter and a custom Predicate. Generally, Predicates are used to color based on contents of the cell, but you could just as easily have it color based on row/column and some external value.

查看 http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org渲染和突出显示下的/jdesktop/swingx/JXTable.html ,这是条带表和模式匹配。你基本上和模式荧光笔做同样的事情,但是使用你自己的谓词。

Look in http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html under Rendering and Highlighting, which is a striped table and pattern matching. You'd essentially do the same thing as the pattern highlighter, but with your own Predicate.

这篇关于JTable使用TableCellRenderer更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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