Robin H.
Robin H.

Reputation: 13

JScrollPane set vertical position not possible

I tried to set the position of a JScroll Pane vertically but it seems like it's capped at some point. Horizontally it works just fine.

    JPanel panel = new JPanel(null);
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setPreferredSize(new Dimension(800, 800));
    JLabel l = new JLabel("test1");
    l.setBounds(40,40,100,100); panel.add(l);
    JLabel l2 = new JLabel("test2");
    l2.setBounds(440,440,100,100);panel.add(l2);


    JScrollPane scroll = new JScrollPane(panel);

    scroll.getVerticalScrollBar().setValue(500);
    scroll.getHorizontalScrollBar().setValue(500);


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(scroll, BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);

picture: https://i.sstatic.net/utpPn.png

I can set the vertical number to any I want. It wont go beyond this position.

Hopefully someone can help me. Ty already.

Upvotes: 1

Views: 922

Answers (1)

Dani
Dani

Reputation: 2036

try this:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class VerticalScrollPane {

    public static void main(String[] args) {        
        JPanel panel = new JPanel(null);
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setPreferredSize(new Dimension(800, 800));
        panel.setVisible(true);

        JLabel l = new JLabel("test1");
        l.setBounds(40,40,100,100); panel.add(l);
        JLabel l2 = new JLabel("test2");
        l2.setBounds(440,440,100,100);panel.add(l2);

        JScrollPane scroll = new JScrollPane(panel);        
        scroll.getViewport().setViewPosition(new Point(500,350)); //  Use this

        JFrame frame = new JFrame();
        frame.getContentPane().add(scroll);
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(scroll, BorderLayout.CENTER);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

Result:

Vertical ScrollPane Java

Upvotes: 1

Related Questions