I have a JFrame
containing a JScrollPane
containing a JPanel
.
The JPanel
contains a bunch of JTextArea
s.
I\
The problem had nothing to do with thread safety. After much digging, I found that the underlying window manager implementation for the Look And Feel of JPanel
had a hardcoded 32767 size limit, so it didn't matter that it was sitting in a JScrollPane
. The idea was to avoid a lot of extra managing of individual JScrollPane
s but the limit makes it unavoidable.
Moving the contents of the JPanel
into their own individual JscrollPane
s resolved the issue. The scrolling is still a bit laggy for reasons unknown to me.
Your example is incorrectly synchronized in that it updates jTextArea2
on the initial thread. Note that JTextArea#setText() is no longer thread safe. The example below invokes EditorKit#read()
, as suggested here, to load the same 27 MB, 176 K line file examined here. This takes a few seconds, about about twice as long as the JTable
approach, but scrolling is comparable.
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* @see https://stackoverflow.com/a/25691384/230513
*/
public class Test {
private static final String NAME = "/var/log/install.log";
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea text = new JTextArea(24, 32);
try {
text.read(new BufferedReader(new FileReader(NAME)), null);
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
f.add(new JScrollPane(text));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Test().display();
});
}
}