user17370683
user17370683

Reputation:

setting a JScrollBar in the middle

I have a JList inside a JFrame, if enough items are added to the JList it shows a vertical or horizontal ScrollBar. The problem I am facing is when the ScrollBars appear they start at value 0. I want to start the thumb in the middle every-time it appears. I have tried and coded this part:

JList list = new JList();

JScrollPane scrollPane = new JScrollPane();
JScrollBar hScrollBar = scrollPane.getHorizontalScrollBar();

scrollPane.getViewport().addChangeListener(e -> SwingUtilities.invokeLater(() -> {
            if (isVisible && !scrollBar.isVisible())
                isVisible = false;
            else if (!isVisible && scrollBar.isVisible()){
                isVisible = true;
                scrollBar.setValue(scrollBar.getMaximum() / 2);
            }
}));

the problem I am having with this implementation is that scrollBar.getMaximum / 2 doesn't get me to the middle of the ScrollBar. It brings me to the end of ScrollBar.

How would I achieve such a thing?

have a great day!

Upvotes: 0

Views: 20

Answers (0)

Related Questions