簡體   English   中英

如何在 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 正確顯示序列

300,000 個字符序列但是,當序列超過 400,000 個字符時,JTextArea 上顯示的序列將被覆蓋且難以辨認100 萬個字符序列如何在 JTextArea 中顯示非常大的字符串而不破壞這些大(> = 400,000 個字符)字符串?

我的代碼:

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 渲染管道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM