Swing JScrollPane - how to set vertical scroll bar to left?

后端 未结 1 1531
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 16:04

See the title. I\'ve tried to change component orientation to RIGHT_TO_LEFT, but that had a unexpected side effect - it behaves strangely with components with speci

相关标签:
1条回答
  • 2021-01-21 16:46

    Agree with the OP: it's bug. The setting of RToL orientation triggers the calculation of the viewPosition. This happens before the layout is done. In that case, the JViewport returns the view's preferredSize instead of its actual size (which at that time is zero). Deep in the bowels, the BasicScrollPaneUI.Handler doesn't know about that faked size and bases its calculation on incorrect data.

    Here's some code to play with (the OPs original stripped down a bit further, added colored borders to see where is what):

    public class COInScrollPane extends JFrame {
    
        public COInScrollPane(){
            super();
            initialize();
        }
    
        private void initialize(){
            this.setTitle("JFrame");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JScrollPane jMainScrollPane = getJMainScrollPane();
            this.add(jMainScrollPane);
            this.setSize(480, 339);
            Action action = new AbstractAction("Toggle CO") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    ComponentOrientation co = jMainScrollPane.getComponentOrientation().isLeftToRight() ?
                            ComponentOrientation.RIGHT_TO_LEFT : ComponentOrientation.LEFT_TO_RIGHT;
                    jMainScrollPane.applyComponentOrientation(co);
                    jMainScrollPane.revalidate();
                    jMainScrollPane.repaint();
                }
            };
            add(new JButton(action), BorderLayout.SOUTH);
        }
    
        private JScrollPane getJMainScrollPane() {
            JScrollPane jMainScrollPane = new JScrollPane(getJMainPanel());
            jMainScrollPane.setViewportBorder(BorderFactory
                    .createLineBorder(Color.GREEN));
            jMainScrollPane
                    .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            return jMainScrollPane;
        }
    
        private JPanel getJMainPanel() {
            JPanel jMainPanel = new JPanel();
            jMainPanel.add(new JButton("just a button"));
            jMainPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
            return jMainPanel;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new COInScrollPane().setVisible(true);
    
                }
            });
        }
    }
    

    The initial layout is completely wrong (as reported by OP), toggle the CO with the button twice - the Layout looks okay.

    A quick and dirty hack might be to do just that in procduction context: after initial layout, force the CO into LToR to allow for correct coordinates then back to RToL.

    0 讨论(0)
提交回复
热议问题