JTable with Thread throw java.lang.ArrayIndexOutOfBoundsException

时间:2016-01-07 18:37:56

标签: java multithreading swing

我想使用Thread修改我的JTable,但有时我会获得 java.lang.ArrayIndexOutOfBoundsException ...

这里是我的代码

//... Ini Declaration
javax.swing.JTable theJfTable = new javax.swing.JTable();
theJfTable.setModel(new javax.swing.table.DefaultTableModel(
  new Object [][] {

  },
  new String [] {
    "Freqs", "Real", "Imag", "Mod", "Angle"
  }
));
//... End Declaration

我正在创建一个帖子,因为这个过程需要很长时间! 我想避免我的程序被冻结然后我将使用一个线程...

  Runnable fillData = new Runnable() {
        public void run() {
            double[] bins = ... //  DoubleArrayWithSizeF;
            double[] Real = ... //  DoubleArrayWithSizeF;
            double[] Imag = ... //  DoubleArrayWithSizeF;
            double[] Mod = ... //  DoubleArrayWithSizeF;
            double[] Arg = ... //  DoubleArrayWithSizeF;

            int iSizeF = bins.length;

            ((DefaultTableModel) theJfTable.getModel()).setRowCount(0);
            for (int i = 0; i < iSizeF; i++) {
              ((DefaultTableModel) theJfTable.getModel()).addRow(new Object[]{ // Line 9208 Problem Here!!!!
                  String.format("%.2f", bins[i]),
                  String.format("%.2f", Real[i]),
                  String.format("%.2f", Imag[i]),
                  String.format("%.2f", Mod[i]),
                  String.format("%.2f", Arg[i])
              });
            }
        }
  };
  new Thread(fillData).start();

例外!!!

at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:265)
at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:375)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:350)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:361)
at JA_JF_BinFileNumbers.JF_BinFileAndNumbers$80.run(JF_BinFileAndNumbers.java:9208)
at java.lang.Thread.run(Thread.java:745)

我想避免错误...我想使用Try Catch但我的结果不安全!!!

PD: Populate JTable with large number of rows

使用SwingWorker不是唯一的答案......

1 个答案:

答案 0 :(得分:0)

更简单的解决方案。

  Runnable fillData = new Runnable() {
        public void run() {
            double[] bins = ... //  DoubleArrayWithSizeF;
            double[] Real = ... //  DoubleArrayWithSizeF;
            double[] Imag = ... //  DoubleArrayWithSizeF;
            double[] Mod = ... //  DoubleArrayWithSizeF;
            double[] Arg = ... //  DoubleArrayWithSizeF;

            int iSizeF = bins.length;

            Object[][] matrix = new Object[bins.length][5];
            for (int row = 0; row < matrix.length; row++) {
                matrix[row][0] = String.format("%.2f", bins[row]);
                matrix[row][1] = String.format("%.2f", Real[row]));
                matrix[row][2] = String.format("%.2f", Imag[row]));
                matrix[row][3] = String.format("%.2f", Mod[row]));
                matrix[row][4] = String.format("%.2f", Arg[row]);
            }
            String[] headers = {"Freqs", "Real", "Imag", "Mod", "Angle"};

            final DefaultTableModel dtm = new DefaultTableModel(matrix,headers);
            SwingUtilities.invokeLater(
                    new Runnable() {
                        @Override
                        public void run() {
                            theJfTable.setModel(dtm);
                        }
                    }
            );

        }
  };
  new Thread(fillData).start();