【问题标题】:Code to set JTable row to different colors not working将 JTable 行设置为不同颜色的代码不起作用
【发布时间】:2015-12-10 19:50:20
【问题描述】:

我开发了下面列出的代码,它没有将行的颜色设置为黄色,从数据库表中检索到的相关时间戳值大于 1.5 秒(1500 毫秒)上一行。我将只提供代码的核心内容,让您不必查看所有行,例如调试输出、日志记录异常等。

private void processQueryResults(ResultSet results){
    Vector<Vector<String>> resultRows = new Vector<Vector<String>>();
    Vector<String> resultRowVector = null;
    Vector<Timestamp> timestampVectors = null;

    resultRows.clear();
    if (results != null){
       timestampVectors = new Vector<Timestamp>();
       While (results.next()){
          resultRowVector = new Vector<String>();
          timestampVectors.add(results.getTimestamp("timestamp_column");

          resultRowVector.add(results.getString("columnA");
          resultRowVector.add(results.getString("columnB");
          .....
          .....
          resultRows.add(resultRowVector);
       }
       JTable displayTable =
             highlightResultRows(new DefaultTableModel(resultRows, colHdrs),
                                 timestampVectors);
       displayTable.setPreferredScrollableViewportSize(new Dimension(900,500));
       ......
       ......
    }

    private JTable highlightResultRows(DefaultTableModel model,
                                       final Vector<Timestamp> timestamps){
        Timestamp previousTimestamp = null;
        JTable highlightedTable = new JTable(model){
           private static final long serialVersionUID = 1L;
           public Component prepareRenderer(TableCellRenderer renderer, int row, int col){
               Component comp = super.prepareRenderer(renderer, row, col);
               if (previousTimestamp != null){
                   if (previousTimestamp.getTime() < (timestamps.get(row).getTime() - 1500)){
                       comp.setBackground(Color.YELLOW);
                   }
                   else
                   {
                       comp.setBackground(Color.BLUE);
                   }
               }
               else
               {
                  comp.setBackground(Color.BLUE);
               }
               previousTimestamp = timestamps.get(row);
               System.out.println("previousTimestamp: [" + previousTimestamp.getTime() + "]");

               return comp;
           }
        };
        return highlightedTable;
    }

我知道没有发生的是prepareRenderer() 方法中的逻辑没有执行,因为debus System.out.println 显示previousTimestamp 设置的内容没有被输出。此外,所有行都设置为蓝色背景色。

【问题讨论】:

  • “我将只提供代码的核心内容,让您不必查看所有行” 为了尽快获得更好的帮助,请发布 minimal reproducible exampleShort, Self Contained, Correct Example .
  • Andrew 我提供了我认为帮助解决此问题所需的最少代码。提供的代码是我觉得作为软件工程师可能需要提供解决方案的需要。明显的问题是方法highlghtResultRows,它是按书面形式完整提供的。我提供了生成此方法的输入参数的代码子集。不是 100% 确定还需要什么。
  • “安德鲁,我提供了我认为帮助解决此问题所需的最少代码”判断是否包含相关代码。 OTOH,我链接到了一个文档,该文档的作者为新手解决了许多问题,并且知道当包含可运行代码时,提供帮助会变得多么容易。但是,嘿,这是你的问题,所以你可以随心所欲地处理它。我只会投票关闭。祝你好运!

标签: java swing jtable


【解决方案1】:

在这种情况下,您可以引入如下方法,您可以将 jtable 作为参数传递:

public void filterRowsWithDifferentColor(JTable table) {

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (!isSelected) {
                try {//assuming the time displaying in your table column 3 with the given format (11:30)  you can change it as you need
                    Time timeValue = new Time(new SimpleDateFormat("HH:mm")
                            .parse(table.getValueAt(row, 3).toString()).getTime());

                    long startTime = timeValue.getTime();

                    long endTime = System.currentTimeMillis();
                    long differenceTime = endTime - startTime;

                    if (TimeUnit.MILLISECONDS.toMillis(differenceTime) > 1500) {

                        c.setBackground(Color.YELLOW);

                    }
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }

            }
            return c;
        }
    });

}

【讨论】:

  • 马杜山感谢建议的代码。在我的情况下,需要更改以将两个连续表行的时间值相互比较,而不是与当前时间进行比较。如果用户向前/向下滚动则有效,但如果用户向后/向上滚动,我还没有找到解决方案。如果当前进程行(第 n-1 行)的时间值至少提前 1500 毫秒,则无法确定如何将先前处理的行(第 n 行)单元格的颜色设置为黄色。以前处理的行不再可用于更新。任何额外的帮助将再次受到赞赏。谢谢,罗素
猜你喜欢
  • 2019-05-22
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 2022-01-14
相关资源
最近更新 更多