[英]Highlight a cell in JTable with an Icon in it
I have a JTable with 3 columns in it. 我有一个3列的JTable。 1. Icon, 2. Name of file or folder, 3. File type or "Folder".
1.图标,2.文件或文件夹的名称,3.文件类型或“文件夹”。 I draw the Icon using a JLabel (I set background + png image) within the
getTableCellRendererComponent
method. 我在
getTableCellRendererComponent
方法中使用JLabel(我设置了背景+ png图像)绘制了Icon。 Initially I draw an alternating background of the JLable either "white" or "grey" since those are the colors that the JTable
Swing component alternates to draw the table. 最初,我绘制JLable的“白色”或“灰色”的交替背景,因为这些是
JTable
Swing组件交替绘制表的颜色。 Now, when I select a row, the Icon (the first column) background does not get redrawn to "dark blue" same as the rest of the row. 现在,当我选择一行时,与该行的其余部分一样,图标(第一列)的背景不会重绘为“深蓝色”。
Here are my questions: 这是我的问题:
General 1) How can also highlight the Icon cell when highlighting a row (pointers will suffice, no code expected)? 常规 1)在突出显示一行时,如何还突出显示Icon单元格(指针就足够了,不需要代码)?
Specific 具体
1.1) Do I have to use JLabel? 1.1)我必须使用JLabel吗? Why can't I just eg
.SetValueAt("image.png",0,0)
为什么我不能仅仅例如
.SetValueAt("image.png",0,0)
1.2) I tried the getColumnClass(...)
but that seems to redraw ALL cells in a given column. 1.2)我尝试了
getColumnClass(...)
但这似乎重绘了给定列中的所有单元格。 Is that expected? 这是预期的吗?
Thanks. 谢谢。
1.) The javax.swing.table.TableCellRenderer
gets an isSelected
parameter when it is called. 1.)调用
javax.swing.table.TableCellRenderer
会获得一个isSelected
参数。 You can easily write your own TableCellRenderer
by inheriting from JLabel
(for example) and overridding getTableCellRendererComponent
: Adjust the Object and return this
. 您可以通过继承
JLabel
(例如)并覆盖getTableCellRendererComponent
来轻松编写自己的TableCellRenderer
:调整Object并返回this
。 Having your own renderer also allows you to set a breakpoint and really understand what is happening. 拥有自己的渲染器还可以让您设置断点并真正了解正在发生的事情。
1.1 + 1.2.) Both setValueAt
and getColumnClass
are part if the model and will probably not solve your problem with the selected background. 1.1 + 1.2。)
setValueAt
和getColumnClass
都是模型的一部分,可能无法解决所选背景下的问题。
You do not have to use JLabel
: If you look at the return type from getTableCellRendererComponent
you notice it is Component
(not even JComponent
). 您不必使用
JLabel
:如果查看getTableCellRendererComponent
的返回类型,您会注意到它是Component
(甚至不是JComponent
)。 I guess JLabel
is just customary because it normally has all the features you want for the renderer and the DefaultTableCellRenderer
also uses JLabel
. 我猜
JLabel
只是习惯,因为它通常具有渲染器所需的所有功能,并且DefaultTableCellRenderer
也使用JLabel
。 For the most freedom I advise you to use JComponent
and write your own paintComponent
, but in this case you probably don't have to do that. 为了获得最大的自由度,我建议您使用
JComponent
并编写自己的paintComponent
,但是在这种情况下,您可能不必这样做。
assuming the striping (alternate white/grey background) happens automatically in your LAF (Nimbus?), you shouldn't need a custom renderer: the table already has a default renderer registered for Icon and ImageIcon class. 假设在LAF(Nimbus?)中自动发生条纹(背景为白色/灰色),则不需要自定义渲染器:该表已经为Icon和ImageIcon类注册了默认渲染器。 Make sure your tableModel returns one of those classes as columnClass for the first column and enjoy the automatics :-)
确保您的tableModel返回其中一个类作为columnClass作为第一列,并享受自动操作:-)
Here's a quick code snippet: 这是一个快速的代码片段:
DefaultTableModel model = new DefaultTableModel(0, 2) {
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Icon.class;
}
return super.getColumnClass(columnIndex);
}
};
File[] files = new File(".").listFiles();
FileSystemView fsv = FileSystemView.getFileSystemView();
for (File f : files) {
model.addRow(new Object[] {fsv.getSystemIcon(f), fsv.getSystemDisplayName(f)});
}
JTable table = new JTable(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.