如何将渲染器仅应用于 JTable 中的特定单元格而不是整个列?
我对 Swing 很陌生。我有一个 JTable,其中每个单元格中都显示图像。我需要仅在当前选定的单元格周围创建红色边框。为此,我使用了以下渲染器类:
public class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl=new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column )
{
lbl.setIcon((ImageIcon)value);
if(isSelected && hasFocus)
{
lbl.setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.yellow));
}
return lbl;
}
}
我面临的问题是,当我单击 JTable 中的任何单元格时,将显示给定列的所有单元格的边框,而不是该特定单元格。我只需要选定单元格周围的边框,而不需要该特定列中存在的所有单元格周围的边框。
I am quiet new to Swing. I have a JTable in which images are displayed in each cell. I need to create a RED border only around the cell which is currently selected. To do this I used following renderer class:
public class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl=new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column )
{
lbl.setIcon((ImageIcon)value);
if(isSelected && hasFocus)
{
lbl.setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.yellow));
}
return lbl;
}
}
The problem I am facing is that when I click on any cell in the JTable then instead of that particular cell the border is displayed for all the cells of the given column. I only need the border around the selected cell and not around all the cells present in that particular column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果未选择单元格,您是否尝试取消设置边框?
Did you try to unset the border if the cell is not selected?