【问题标题】:change the color of specific rows every 10 seconds每 10 秒更改特定行的颜色
【发布时间】:2021-11-16 15:08:08
【问题描述】:

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

 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;
      }
}    '''

【问题讨论】:

    标签: java swing colors jtable rows


    【解决方案1】:

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

    【讨论】:

    • 我在 netbeans 中使用 gui builder 在哪里可以定义表格单元格渲染器?它会重新粉刷整个框架还是只重新粉刷桌子所在的面板?我应该把它放在 timertask 里面还是什么?
    • 我也喜欢 Netbeans,它是 gui 构建器。但是,我不确定仅使用 gui 构建器可以走多远。在 initComponents() 运行后,我只需将 TableCellRenderer 添加到 JTable。至于重绘多少:UI系统实际决定。通过在组件上调用 repaint 方法,您表明该区域应该被重绘。因此,我会在尽可能小的组件上执行此调用 - 即 JTable。 CellRenderer 用作享元模式,因此无法在较小的级别上工作。
    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2017-09-16
    • 2020-11-05
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多