Scrolling limitation with JScrollPane and JViewport maximum sizes smaller than contents

后端 未结 2 1988
礼貌的吻别
礼貌的吻别 2021-01-20 21:39

I have a JFrame containing a JScrollPane containing a JPanel. The JPanel contains a bunch of JTextAreas. I\

2条回答
  •  面向向阳花
    2021-01-20 22:37

    Your example is incorrectly synchronized in that it updates jTextArea2 on the initial thread. Note that JTextArea#setText() is no longer thread safe. The example below invokes EditorKit#read(), as suggested here, to load the same 27 MB, 176 K line file examined here. This takes a few seconds, about about twice as long as the JTable approach, but scrolling is comparable.

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

提交回复
热议问题