我的JFrame
包含JScrollPane
,其中包含JPanel
。
JPanel
包含一堆JTextArea
个。
我正在加载大量文本(约8k-10k字符)。
布局工作正常,但滚动有点滞后。
真正的问题是JPanel
,JScrollPane
和JViewport
似乎有32767大小的限制,所以当任何JTextArea
增长高于此值时,它就可以' t进一步滚动显示文本的最后1/3。
下面您可以看到问题的最小示例。我使用了NetBeans JFrame
设计器,因此可能有点冗长,但我从默认设置中更改的唯一内容是JTextArea
是JPanel
的直接子项,滚动条策略,以及略大的字体大小:
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTextArea1 = new javax.swing.JTextArea();
jTextArea2 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
jTextArea1.setRows(5);
jTextArea2.setColumns(20);
jTextArea2.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
jTextArea2.setRows(5);
jTextArea2.setText("Some long text...");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jTextArea1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextArea2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextArea1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
.addComponent(jTextArea2))
.addGap(0, 0, 0))
);
jScrollPane1.setViewportView(jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
final NewJFrame f = new NewJFrame();
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}catch(ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
f.setVisible(true);
}
});
StringBuilder txt = new StringBuilder();
for(int i=0; i<10000; i++)
txt.append(i).append("\n");
f.jTextArea1.setText(txt.toString());
txt = new StringBuilder();
txt.append("JTextArea height: ").append(f.jTextArea1.getHeight()).append('\n');
txt.append("JTextArea rows: ").append(f.jTextArea1.getRows()).append('\n');
txt.append("JScrollPane height:").append(f.jScrollPane1.getHeight()).append('\n');
txt.append("JViewport height:").append(f.jScrollPane1.getViewport().getHeight()).append('\n');
txt.append("JPanel height:").append(f.jPanel1.getHeight()).append('\n');
f.jTextArea2.setText(txt.toString());
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// End of variables declaration
}
如果你运行它并滚动到底部,你可能会看到计数达到10 000,但它只会达到1637,你可以看到下一行的顶部像素几乎没有出现。
我已尝试在setMaximumSize
,setSize
及其JPanel
上JScrollPane
和JViewport
但未发生任何变化。我也有些困惑,即使有10k行文本,其中一些可以滚动到足以观看,getRows()
和getSize()
方法返回原始值。
当我想要一个大于32767的可滚动JTextArea
时,处理这种情况的正确方法是什么?
答案 0 :(得分:2)
您的示例未正确同步,因为它更新initial thread上的jTextArea2
。请注意JTextArea#setText()
不再是线程安全的。下面的示例根据建议here调用EditorKit#read()
,以加载检查here的相同27 MB,176 K行文件。这需要几秒钟,大约是JTable
方法的两倍,但滚动是可比的。
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();
});
}
}
答案 1 :(得分:1)
问题与线程安全无关。经过大量挖掘后,我发现JPanel
外观的底层窗口管理器实现具有硬编码的32767大小限制,因此它位于JScrollPane
并不重要。我们的想法是避免对个别JScrollPane
进行大量额外管理,但限制使其无法避免。
将JPanel
的内容移动到他们自己的个人JscrollPane
中解决了这个问题。由于我不知道的原因,滚动仍然有点滞后。