Scrolling limitation with JScrollPane and JViewport maximum sizes smaller than contents

后端 未结 2 1984
礼貌的吻别
礼貌的吻别 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:36

    The problem had nothing to do with thread safety. After much digging, I found that the underlying window manager implementation for the Look And Feel of JPanel had a hardcoded 32767 size limit, so it didn't matter that it was sitting in a JScrollPane. The idea was to avoid a lot of extra managing of individual JScrollPanes but the limit makes it unavoidable.

    Moving the contents of the JPanel into their own individual JscrollPanes resolved the issue. The scrolling is still a bit laggy for reasons unknown to me.

    0 讨论(0)
  • 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();
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题