调整JTable中的一个单元而不是整行(Adjust one cell in JTable instead of whole row)

编程入门 行业动态 更新时间:2024-10-27 00:23:35
调整JTable中的一个单元而不是整行(Adjust one cell in JTable instead of whole row)

我遇到了一个小问题,我正在努力解决它。 基本上发生的事情是我有一个JTable,它由一个我从API调用获得的数组填充。 我目前所拥有的是,如果设备显示为在线,它将改变绿色,如果离线,则为浅灰色。 问题是它影响整个ROW,而不仅仅是状态CELL。 我只希望状态单元格突出显示绿色。 任何帮助将非常感激。

custTable = new javax.swing.JTable(model){ @Override public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int columnIndex) { JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex); if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Online")) { component.setBackground(Color.GREEN); } else if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Offline")) { component.setBackground(Color.lightGray); } return component; }

I have run into a slight issue and am struggling to resolve it. Basically what is happening is that I have a JTable which is being populated by an array that I get from an API call. What I currently have is that if a device shows as online it will change GREEN, if offline, then a light-gray. The problem is that it is affecting the entire ROW and not just the status CELL. I only want the status cell to highlight green. Any help would be really appreciated.

custTable = new javax.swing.JTable(model){ @Override public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int columnIndex) { JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex); if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Online")) { component.setBackground(Color.GREEN); } else if(getValueAt(rowIndex,1).toString().equalsIgnoreCase("Offline")) { component.setBackground(Color.lightGray); } return component; }

最满意答案

不要重写prepareRenderer()方法。 通常,只有在希望渲染对输入行有效时才会覆盖此方法。 此方法很有用,因为渲染代码位于一个位置,您不必为表中的每个列创建自定义渲染器。

但是,对于特定列中单元格的特定渲染,您应该为该列创建自定义渲染器。

阅读使用自定义渲染器的Swing教程中的部分以获取更多信息和示例。

本教程示例实现了TableCellRenderer接口。 扩展默认渲染器可能更容易:

class ColorRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (!isSelected) { int viewColumn = convertColumnIndexToView(1) String value = getModel().getValueAt(rowIndex, viewColumn).toString(); if ("Online".equalsIgnoreCase(value)) setBackground( Color.GREEN ); else setBackground( Color.lightgray ); return this; } } }

请注意,如果用户重新排序表中的列,则应转换列索引。

Don't override the prepareRenderer() method. Generally you would only override this method when you want the rendering to be effective for the enter row. This approach is useful because the rendering code is in one place and you don't have to create custom renderers for every column in the table.

However, for specific rendering of a cell in a specific column you should create a custom renderer for the column.

Read the section from the Swing tutorial on Using Custom Renderers for more information and examples.

The tutorial example implement the TableCellRenderer interface. It may be easier to extend the default renderer:

class ColorRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (!isSelected) { int viewColumn = convertColumnIndexToView(1) String value = getModel().getValueAt(rowIndex, viewColumn).toString(); if ("Online".equalsIgnoreCase(value)) setBackground( Color.GREEN ); else setBackground( Color.lightgray ); return this; } } }

Note you should convert the column index in case the user has reorder the columns in the table.

更多推荐

本文发布于:2023-08-05 08:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   单元   JTable   row   cell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!