编程语言
首页 > 编程语言> > java – 动态地向JTable添加行 – 为什么它们一次出现?

java – 动态地向JTable添加行 – 为什么它们一次出现?

作者:互联网

在这个例子中,我试图在我的GUI中添加一个表,然后动态地向其添加行(以显示进度).我不明白为什么所有的行都出现在一起.我的意思是,桌子的变化,不是吗?有人可以给我一个解释吗?

import java.awt.Component;

public class Main {
  public static void main(String[] args) {
    // Show GUI
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        GUI gui = new GUI();
        gui.setVisible(true);

        DefaultTableModel model = new DefaultTableModel(
          new String[] { "Column 1", "Column 2" }, 0);
        JTable table = new JTable(model);

        gui.add(table);
        gui.validate();

        for (int i = 0; i < 10; i++) {
          System.out.println("Row " + i);
          model
            .addRow(new String[] { "Row", String.valueOf(i) });
          // model.fireTableDataChanged();

          try {
            Thread.sleep(250);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

        }
      }
    });
  }
}

class GUI extends JFrame {
  private static final long serialVersionUID = 1L;

  public GUI() {
    setTitle("GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 350, 100);
    setLocationRelativeTo(null);

    JPanel cp = new JPanel();
    cp.setBorder(new EmptyBorder(10, 10, 10, 10));
    setContentPane(cp);
  }
}

解决方法:

重申Kleopatra:不要睡觉EDT

您可以改为使用javax.swing.Timer,如this answer所示

编辑

我不想太乱用你的代码(只是因为它看起来很奇怪)但我改了一下就添加了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++;
    }
}

标签:thread-sleep,swingworker,java,swing,jtable
来源: https://codeday.me/bug/20191002/1844491.html