如何在 JTextArea 中显示非常大的字符串,即超过 100 万个字符?
How to display very large strings i.e exceeding 1 million characters in a JTextArea?
我正在创建一个基本的 Swing GUI,当用户从 JList 中选择序列 ID 时,它会在 JTextArea 中显示非常大的字符串(序列)。当序列字符串长度 <= 300,000 个字符时,JTextArea 正确显示序列
但是,当序列超过 400,000 个字符时,显示在 JTextArea 上的序列将被覆盖且难以辨认
如何在不破坏这些大(>=400,000 个字符)字符串的情况下在 JTextArea 中显示非常大的字符串?
我的代码:
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());
}
}
据报道,此问题是 Linux 环境 (https://bugs.openjdk.java.net/browse/JDK-8262010) 中的一个常见错误。
我使用
解决了这个问题
-Dsun.java2d.xrender=false
Java 程序时 运行 VM 选项,关闭基于 XRender 的 Java 2D 渲染管道。
我正在创建一个基本的 Swing GUI,当用户从 JList 中选择序列 ID 时,它会在 JTextArea 中显示非常大的字符串(序列)。当序列字符串长度 <= 300,000 个字符时,JTextArea 正确显示序列
我的代码:
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());
}
}
据报道,此问题是 Linux 环境 (https://bugs.openjdk.java.net/browse/JDK-8262010) 中的一个常见错误。
我使用
解决了这个问题-Dsun.java2d.xrender=false
Java 程序时 运行 VM 选项,关闭基于 XRender 的 Java 2D 渲染管道。