Search code examples
javaswingjscrollpanejscrollbar

How to set horizontal scroll pane starting location on the left end?


My code for JScrollPane are as follow :-

JScrollPane sp = new JScrollPane (textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

The issue I have is that when i run the program the scroll bar always starts from the right end which is the ending of the sentence.

enter image description here


Solution

  • @sasikumar's answer is correct. It just needs a small adaption in order to be sure that the JScrollBar will get its value changed. Try to call it like that:

    SwingUtilities.invokeLater(() -> {
        JScrollBar horizontalScrollBar = sp.getHorizontalScrollBar();
        horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
    });