将JTable行设置为不起作用的不同颜色的代码

时间:2015-12-10 19:50:20

标签: java swing jtable

我开发了下面列出的代码,但是它不能将行的颜色设置为黄色,而从数据库表中检索到的关联时间戳值大于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()方法中的逻辑没有执行,因为显示previousTimestamp设置的debus System.out.println没有输出。此外,所有行都设置为蓝色背景颜色。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以引入如下方法,您可以将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;
        }
    });

}
相关问题