0

I want to modify my JTable using a Thread, but sometimes I obtaining java.lang.ArrayIndexOutOfBoundsException...

Here my code

//... 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

I'm creating a thread because the process take a long long time!!! I want to avoid my programm get freezed then I will use a Thread...

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

The Exception!!!

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)

I want to avoid the error... I was thinking to use a Try Catch but my results will be not secure!!!

PD: Populate JTable with large number of rows

Using SwingWorker is not the only answer...

Community
  • 1
  • 1
Anita
  • 1,217
  • 1
  • 14
  • 27
  • What does *I was thinking to use a Try Catch but my results will be not secure!!!* mean?, also in your answer, updating the component is still done by the external thread not EDT. – Plain_Dude_Sleeping_Alone Jan 07 '16 at 18:58
  • Start by having, another, read of [this](http://stackoverflow.com/questions/13407418/threads-and-jtable/13407504#13407504) and [this](http://stackoverflow.com/questions/25519336/jtablemodellistener-tablechanged-thread-safe/25519488#25519488) – MadProgrammer Jan 07 '16 at 21:41
  • Then see [this for a possible solution](http://stackoverflow.com/questions/17414109/populate-jtable-with-large-number-of-rows/17415635#17415635) – MadProgrammer Jan 07 '16 at 21:43
  • *"PD: Populate JTable with large number of rows - Using SwingWorker is not the only answer..."* and what answer do you want? What is `SwingWorker` not the appropriate in this case? It uses a background thread to allow you to populate the table and update the UI safely via `publish/process` and will prevent the exception you are getting? – MadProgrammer Jan 07 '16 at 21:48

1 Answers1

0

A Simpler solutions.

  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();
Anita
  • 1,217
  • 1
  • 14
  • 27