Dynamically adding rows to JTable - Why do they appear at once?

后端 未结 3 418
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 22:55

In the example, I\'m seeking to add a table to my GUI and then dynamically add rows to it (to show the progress). What I don\'t understand is why all the rows are appearing

3条回答
  •  再見小時候
    2020-12-21 23:55

    Reiterating Kleopatra : Don't Sleep the EDT

    You can instead use a javax.swing.Timer as seen in this answer

    enter image description here


    EDIT

    I didn't want to mess with your code too much (just because it looks weird to me) but I changed it somewhat to add the Timer

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.Timer;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.DefaultTableModel;
    
    public class Main {
    
        static JTable table;
        static GUI gui;
        static Processor p = null;
    
        public static void main(String[] args) {
            // Show GUI
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    gui = new GUI();
    
                    p = new Processor() {
    
                        @Override
                        public void execute() {
                            final JTable table = new JTable(p.getTableModel());
                            final JScrollPane scrollPane = new JScrollPane(table);
                            gui.getContentPane().add(scrollPane, BorderLayout.CENTER);
                            gui.setLocationRelativeTo(null);
                            gui.setVisible(true);
    
                            Timer timer = new Timer(100, new ActionListener(){
                                public void actionPerformed(ActionEvent e) {
                                    p.processRow();
                                    table.scrollRectToVisible(table.getCellRect(table.getRowCount() - 1, 0, true));
                                }
                            });
                            timer.start();
                        }
                    };
                    p.execute();
                }
            });
        }
    }
    
    class GUI extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public GUI() {
            setTitle("GUI");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 350, 400);
    
            JPanel contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
            contentPane.setLayout(new BorderLayout(0, 0));
    
            setContentPane(contentPane);
        }
    }
    
    interface Callback {
    
        void execute();
    }
    
    abstract class Processor implements Callback {
    
        private final String[] cols = {"COL", "COL", "COL", "COL", "COL"};
        private DefaultTableModel tableModel;
        int numRows;
        int numCols;
        int a, b, c, d, e;
    
        Processor() {
            a = 1; b = 2; c = 3; d = 4; e = 4;
            numRows = 1000;
            tableModel = new DefaultTableModel(cols, numCols);
        }
    
        public DefaultTableModel getTableModel() {
            return tableModel;
        }
    
        public void processRow() {
            tableModel.addRow(new Object[]{a, b, c, d, e});
            a++; b++; c++; d++; e++;
        }
    }
    

提交回复
热议问题