首页 > 解决方案 > 每 10 秒更改特定行的颜色

问题描述

你好,我遇到了一个问题,我需要将 jtabl 的一些行涂成红色,每 10 秒刷新一次。还有一组行颜色的变化。我更新要更改为读取的行集,然后我需要在找到时更改表中的行。我尝试了表格的单元格渲染器,但没有。如果有人可以提出一些我对一切都开放的东西,我很想得到一些代码作为例子。任何方式都会受到欢迎。我尝试玩这个功能,但我无法添加要更改的行集..'''

 class MyCellRenderer extends DefaultTableCellRenderer {
     String separatedVariable;
     public MyCellRenderer(String separatedVariable) {
         this.separatedVariable = separatedVariable;
      }

      @Override
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
          Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
          c.setBackground(Color.WHITE);
          c.setForeground(Color.BLACK);
              JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
          if (separatedVariable.equals("YOUR VALUE TO GREEN")) {
              l.setBackground(Color.GREEN);

              return l;
          } else {
                     if (separatedValue.equals("YOUR VALUE TO YELLOW")) {
                          l.setBackground(Color.YELLOW);
                          return l;
                      } else if (separatedValue.equals("YOUR VALUE TO RED")) {
                          l.setBaground(Color.RED);
                          return l;
                      }
               }
              return c;
      }
}    '''

标签: javaswingcolorsjtablerows

解决方案


使用您自己的TableCellRenderer来显示数据。此渲染器将能够根据行数据和时间渲染正常或红色背景。那么你只需要一个Swing Timer来调用你的 JTable 的 repaint() 方法。


推荐阅读