3

我在 jframe 中有一个 jpanel,它正在将数据库中的数据显示到 jtable 中。现在我想添加计时器以在一段时间后刷新这个 jtable,但我没有找到在 jpanel 中添加计时器代码的位置。

这是我的代码..

orgR.addMenuListener(new MenuListener() {

        public void menuSelected(MenuEvent e) {


            final JLabel l = new JLabel("Live CDRRecord");
            l.setBounds(320, 50, 500, 60);
            l.setFont((new Font("verdana", Font.BOLD, 25)));



            final Vector columnNames = new Vector();
            final Vector data = new Vector();


            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection con = DriverManager.getConnection("");
                String sql = "Select calldate,source,destination,extension,trunk,duration,toc,callcost from table";
                Statement statement = con.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columns = metaData.getColumnCount();
                columnNames.add("Calldate");
                columnNames.add("Source");
                columnNames.add("Destination");
                columnNames.add("Extension");
                columnNames.add("Trunk");
                columnNames.add("Duration");
                columnNames.add("TOC");
                columnNames.add("CallCost");

                while (resultSet.next()) {
                    Vector row = new Vector(columns);
                    for (int i = 1; i <= columns; i++) {
                        row.addElement(resultSet.getObject(i));
                    }
                    data.addElement(row);
                }
                resultSet.close();
                statement.close();
            } catch (Exception ev) {
                System.out.println(ev);
            }


            final JTable table = new JTable() {

                public Component prepareRenderer(TableCellRenderer renderer,
                        int rowIndex, int vColIndex) {
                    Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
                    if (rowIndex % 2 == 0 && isRowSelected(rowIndex)) {
                        c.setBackground(Color.LIGHT_GRAY);
                    } else {
                        // If not shaded, match the table's background
                        c.setBackground(getBackground());

                    }
                    return c;
                }
            };

            table.setBackground(Color.WHITE);
            table.setModel(new DefaultTableModel(data, columnNames));




            TableColumn column;
            for (int i = 0; i < table.getColumnCount(); i++) {
                column = table.getColumnModel().getColumn(i);
                column.setMaxWidth(800);



            }

            final JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setBounds(10, 120,920,400);


            p2.add(l);

            p2.add(scrollPane);
            p2.setBounds(00, 00, 1400, 650);
            p2.add(menubar);
            p2.setLayout(null);
            Container contentPane = frame.getContentPane();
            contentPane.removeAll();
            contentPane.add(p2);
            contentPane.invalidate();
            contentPane.repaint();


        }

        public void menuDeselected(MenuEvent e) {
            // System.out.println("FileTwo.menuDeselected");
            orgR.removeAll();
        }

        public void menuCanceled(MenuEvent e) {
            // System.out.println("FileTwow.menuCanceled");
            orgR.removeAll();
        }
    });

任何帮助将不胜感激..在此先感谢..

4

2 回答 2

4

确保您使用的是摇摆计时器而不是实用计时器。

public final static int INTERVAL = 1000;

timer = new Timer(INTERVAL, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

   //Refresh the panel
       panel.revalidate();

       if (/* condition to terminate the thread. */) {
        timer.stop();
       }
    }    
});

timer.start();

面板上的 revalidate 方法在指定的 INTERVAL 之后刷新面板。您还应该定义一个终止条件来停止计时器。

在指定的时间间隔之后。计时器触发一个动作事件,该事件由ActionListener该类处理。

于 2013-10-17T11:35:28.100 回答
1

你应该使用javax.swing.Timer,像这样:

    int timerTimeInMilliSeconds = 1000;
    javax.swing.Timer timer = new javax.swing.Timer(timerTimeInMilliSeconds, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            refreshTable();
        }
    });
于 2013-10-17T11:33:14.560 回答