[英]How to display very large strings i.e exceeding 1 million characters in a JTextArea?
I am creating a basic Swing GUI which displays very large strings (sequences) in a JTextArea, when a user selects the sequence ID from a JList.我正在创建一个基本的 Swing GUI,当用户从 JList 中选择序列 ID 时,它会在 JTextArea 中显示非常大的字符串(序列)。 When the sequence string is <= 300,000 characters long, the JTextArea displays the sequence correctly当序列字符串 <= 300,000 个字符长时,JTextArea 正确显示序列
however, when the sequence exceeds 400,000 characters the sequence displayed on the JTextArea is overwritten and illegible但是,当序列超过 400,000 个字符时,JTextArea 上显示的序列将被覆盖且难以辨认How can I display very large strings in a JTextArea without breaking these large (>=400,000 character) strings?如何在 JTextArea 中显示非常大的字符串而不破坏这些大(> = 400,000 个字符)字符串?
My code:我的代码:
public class GUI {
private String[] stringArr;
JList<String> idList;
private JTextArea seqArea;
Map<String, String> sequences;
public void init() {
JFrame frame = new JFrame();
JPanel seqPanel = new JPanel();
stringArr = new String[0];
idList = new JList<>();
idList.addListSelectionListener(new SeqListSelectionListener());
idList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroller = new JScrollPane(idList);
idList.setVisibleRowCount(12);
seqPanel.add(scroller);
idList.setListData(stringArr);
seqArea = new JTextArea(15, 50);
seqArea.setLineWrap(true);
seqArea.setCaretPosition(0);
seqArea.setEditable(false);
JScrollPane seqScroller = new JScrollPane(seqArea);
seqScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
seqScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
seqPanel.add(seqScroller, BorderLayout.WEST);
parseFile();
frame.add(seqPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
private void parseFile() {
// function to simulate string that would be parsed from file.
sequences = new LinkedHashMap<>();
StringBuilder seq1 = new StringBuilder();
StringBuilder seq2 = new StringBuilder();
String[] bases = {"A", "C", "T", "G"};
for (int i=0; i < 400000; i++) {
int index1 = (int) Math.floor(Math.random() * 3);
int index2 = (int) Math.floor(Math.random() * 3);
seq1.append(bases[index1]);
seq2.append(bases[index2]);
}
sequences.put("seq1", seq1.toString());
sequences.put("seq2", seq2.toString());
setList(sequences);
}
private void setList(Map<String, String> sequences) {
stringArr = sequences.keySet().toArray(new String[0]);
idList.setListData(stringArr);
}
public class SeqListSelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent le) {
if (!le.getValueIsAdjusting()) {
String chosenSeq = idList.getSelectedValue();
String sequence = sequences.get(chosenSeq);
seqArea.setText("");
seqArea.setText(sequence);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUI().init());
}
}
This issue is reportedly a common bug in the Linux environment ( https://bugs.openjdk.java.net/browse/JDK-8262010 ).据报道,此问题是 Linux 环境中的常见错误( https://bugs.openjdk.java.net/browse/JDK-8262010 )。 I fixed the issue by using -Dsun.java2d.xrender=false
Java VM option when running the program, which turns off the XRender-based Java 2D rendering pipeline.我通过在运行程序时使用-Dsun.java2d.xrender=false
Java VM 选项解决了这个问题,该选项关闭了基于 XRender 的 Java 2D 渲染管道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.