Java - TaskTimer不适用于cellrenderer Jtable

时间:2017-07-18 02:29:37

标签: java jtable timertask tablecellrenderer

我有一个JTable,其中包含分支的IP地址列表。我尝试扫描所有IP地址并根据ping结果绘制单元格颜色。我使用cellrenderer和Timertask,但是当我运行应用程序冻结并在完成任务后绘制所有单元格时,不像我在TimerTask中看到的那样。我学到了很多话题,但我无法解决问题。 请给我一个建议。 非常感谢你。 我给出下面的代码:

 private void btn_kiemtraActionPerformed(java.awt.event.ActionEvent evt) {                                            

            // TODO add your handling code here:
        timertask = new TimerTask() {
            @Override
            public void run() {                          
                try {
                    kiemtra();
                } catch (IOException | InterruptedException ex) {
                    Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    tm.cancel();
                    tm.purge(); 
                    }
            }

        };
        tm.schedule(timertask, new Date());
}


public void kiemtra() throws IOException, UnknownHostException, InterruptedException {
    CellRenderer mcr = new CellRenderer();
    TableColumnModel columnModel = table_donvi.getColumnModel();
                for (int i = 3 ; i<=4; i++){
                    columnModel.getColumn(i).setCellRenderer(mcr);
                }
}

public Boolean rs (String cmd_ping) throws UnknownHostException, IOException, InterruptedException {
            Process p1 = java.lang.Runtime.getRuntime().exec(cmd_ping);
            int returnVal = p1.waitFor();
            boolean inet = (returnVal==0);
            return inet;
}


public class CellRenderer extends javax.swing.table.DefaultTableCellRenderer {
    public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            String ping = "ping " +table.getValueAt(row, column).toString();
            SwingUtilities.invokeLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                cellComponent.setBackground(java.awt.Color.YELLOW);
                                                table.repaint();
                                            }
                                        });  
            //System.out.println(ping);
        try {
            if (rs(ping)== true) {
                SwingUtilities.invokeLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                cellComponent.setBackground(java.awt.Color.GREEN);
                                                table.repaint();
                                            }
                                        });  

            }
            else {
               SwingUtilities.invokeLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                cellComponent.setBackground(java.awt.Color.RED);
                                                table.repaint();
                                            }
                                        });  
            }
        } catch (IOException ex) {
            Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return cellComponent;
        }
}

0 个答案:

没有答案