简体   繁体   中英

How to set Timer for autoRefresh Jpanel In Java

I have a jpanel in jframe which is displaying data from database into jtable .Now i want to add timer to refresh this jtable after an interval of time but i am not getting where to add the timer code in jpanel..

Here is my code..

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();
        }
    });

Any help will be appreciated..Thanks in advance..

Make sure, you use the swing timer and not the util timer.

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();

The revalidate method on the panel refreshes the panel after the specified INTERVAL. You should also define a terminating condition to stop the timer.

After the specified interval. The timer fires an action event, which is handled by the ActionListener class.

You should use javax.swing.Timer , something like this:

    int timerTimeInMilliSeconds = 1000;
    javax.swing.Timer timer = new javax.swing.Timer(timerTimeInMilliSeconds, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            refreshTable();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM