1

How can I get my JTable to display my data row by row? As my data can be huge, up to thousands of rows, I want to display row by row so the user can see it being populated.

I've tried many different ways like paint, repaint, validate, all the model.fire... and more but still my table only shows the data after everything has been added in at the end. An example of how I'm doing the method:

public void updateTableRowByRow(String row) {
    // length of row always equals cols
    int cols = 16;
    int i = 0;
    DefaultTableModel model = (DefaultTableModel) gui.myTable.getModel();
    Vector<String> rowData = new Vector<String>();
    for (int c = 0; c < cols; c++) {
        rowData.add(String.valueOf(row.charAt(i++)));
    }
    model.addRow(rowData);
    // tried many things here to get it to refresh but nothing worked
}

Before I call this method, I would clear the table:

DefaultTableModel model = (DefaultTableModel) gui.myTable.getModel();
model.setRowCount(0);   // delete all
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
User1915
  • 141
  • 1
  • 4
  • Why don't you implement your own `TableModel` ? – ThomasEdwin Dec 24 '17 at 07:50
  • *"As my data can be huge, up to thousands of rows, I want to display row by row so the user can see it being populated."* Have you actually experimented with large tables? The thing is, while retrieving data might account for 100s of thousands of rows per second, the table itself is smart enough to only render the number of rows it can **display** at that moment, which is typically no more than 40-60. That rendering can be done in the blink of an eye. – Andrew Thompson Dec 24 '17 at 07:51
  • @ThomasEdwin I created a custom table model extending from AbstractTableModel but that didn't work either. I put fireTableDataChanged in the addRow method I wrote. I have no idea how or what I should do to get it to refresh after every row is added in :( – User1915 Dec 24 '17 at 12:56
  • BTW - I did a test creating a 25 column table with data from the Unicode character set. The output was `Character.MAX_CODE_POINT: 1114111 table creation: 3612 entire duration from start-up to GUI appearing: 3853` So.. the GUI appeared in just **241 milliseconds more** than it took to create the table, **for 1,114,111 table rows**. This task smacks of premature optimization. But if you want further help, I suggest you post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) that supports that it is needed. – Andrew Thompson Dec 24 '17 at 14:24
  • I know that it's pretty quick to create a table with lots of rows. My problem is with the pre-processing of the data before adding them into the table. I am reading a file, converting its contents and then adding that content into the table. For this conversion (bin to hex), I am using the most efficient method I can but for large data, this can still take time - which is why I want to display row by row as the conversion takes place. I'll try to get a better example and edit the question again. – User1915 Dec 24 '17 at 14:43
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach. – trashgod Dec 24 '17 at 22:22
  • *"I know that.."* Tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. *"I'll try to get a better example and edit the question again"* Please do that, but as stated by both myself and trashgod, it really needs to be an MCVE. That can be tricky in that it is impossible to include a large text file on SO, so you need to replicate the effect of the loading of a large text file in code. I did that by iterating the 1.1 million Unicode chars in my test example. – Andrew Thompson Dec 25 '17 at 03:43

1 Answers1

0

my table only shows the data after everything has been added in at the end

...

My problem is with the pre-processing of the data before adding them into the table. I am reading a file, converting its contents and then adding that content into the table.

You are doing your processing on the Event Dispatch Thread (EDT) which prevents the GUI from repainting itself until the task is finished.

You need to use a separate Thread for the pre-processing of the data. This will allow the GUI to repaint itself as new data is added to the table model.

The easiest way to do this is to use a SwingWorker. The Swing worker allows you to process data in a separate Thread and then "publish" the data so it can be displayed in the GUI.

Read the section from the Swing tutorial on Concurrency in Swing for more information and working examples.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288