Home > Software engineering >  How to display very large strings i.e exceeding 1 million characters in a JTextArea?
How to display very large strings i.e exceeding 1 million characters in a JTextArea?

Time:01-06

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. When the sequence string is <= 300,000 characters long, the JTextArea displays the sequence correctly

300,000 character sequence however, when the sequence exceeds 400,000 characters the sequence displayed on the JTextArea is overwritten and illegible 1 million character sequence How can I display very large strings in a JTextArea without breaking these large (>=400,000 character) strings?

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());
    }

}



CodePudding user response:

This issue is reportedly a common bug in the Linux environment (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.

  •  Tags:  
  • Related