Reputation: 714
I have a small application that finds "results" really fast (the kind of result is not important here, but sometimes more than 10 per second) and I want to display them to the user in a table-like form or something similar. I use a swing JTable
(inside a JFrame) for that, insert the new result as a new row, and then execute a method to scroll far down again.
Object[] data = new Object[6];
//fill data-array
((DefaultTableModel) resultTable.getModel()).addRow(data);
resultTable.scrollRectToVisible(resultTable.getCellRect(resultTable.getRowCount(), 0, false));
This does the job somehow, but Swing seems to be a bit laggy when updating the view, so that most of the time (due to the huge amount of repaints) the table rows seem to glitch around the window.
Do you have any ideas on how to prevent that from happening? Like syncing with some swing-internal paint locks / using a different method to diplay the results?
Upvotes: 0
Views: 85
Reputation: 285405
Impossible to tell what's wrong without pertinent code, but I'm guessing that you're doing processing that takes time on the Swing event thread (please show more so we don't have to guess). If so, the solution is to use a SwingWorker to allow you to do processing on a background thread while still updating the Swing state on the Swing event thread or EDT. For instance, you could get your data within a loop within the SwingWorker's doInBackground()
method, and then using publish/process to push each row of data onto the JTable's model.
For more on this, please see: Lesson: Concurrency in Swing
If you need more specific help, then please consider creating and posting your Minimal, Complete, and Verifiable Example Program.
Upvotes: 2