Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to a specific location in a JScrollPane

I have a JScrollPane which contains a JPanel which has large height, This large JPanel contain more Jpanels in it as in the image. Some of those panels contains a JLabel which I used to show titles. At the top, there are JLabels which have numbers matching the title numbers in the title labels. What I need to do is when I click a label from the top label list the JScrollBar should scroll to the position where that label is placed.

I don't know whether this is possible or not, but if anyone know how to scroll to a specific position in a JScrollPane please assist me.

enter image description here

like image 255
Harsha Avatar asked Aug 24 '12 02:08

Harsha


People also ask

What is the difference between JScrollPane and JScrollBar?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.

How do you use a JScrollPane?

Create a JTextArea . Call new JScrollPane(textArea) to create a scrollable Text Area. Remember that JScrollPane is a container and you can add any component you want to it to make it scrollable. Use setHorizontalScrollBarPolicy and setVerticalScrollBarPolicy to set the vertical and horizontal scroll bar policies.

How do you set bounds of JScrollPane?

The bounds come down to the position and size of the component. The best way to change the size of a scroll pane is to change the size of the component it is displaying. A text area can be resized by setting the number of rows & columns (easily specified in the constructor), or by setting a different font size.


1 Answers

Assuming you want the whole panel that contains the label as title to be visible:

Container parent = titleLabel.getParent();
parent.scrollRectToVisible(parent.getBounds());

There's no need to access the containing viewport/scrollPane except (there's always an except, isn't it :-)

  • the component the scrollRectToVisible is invoked on has a custom implementation (as f.i. text components
  • if the default location reached by that method isn't up to your liking

Edit

a code snippet for @MadProgrammer :-) - but too lazy to remove every trace of SwingX, so here we go:

final JLabel last = new JLabel("I'm the last");

int maxRow = 20;
int maxColumn = 10;

JComponent content = new JPanel(new GridLayout(maxRow, maxColumn));
for (int row = 0; row < maxRow; row++) {
    for (int column = 0; column < maxColumn; column++) {
        JComponent parent = new JPanel();
        JLabel label = new JLabel("i'm in " + row + "/" + column);
        if (row == (maxRow - 1) && column == (maxColumn - 1)) {
            label = last;
            last.setBorder(BorderFactory.createLineBorder(Color.RED));
        }
        parent.add(label);
        content.add(parent);
    }
}
JXFrame frame = wrapWithScrollingInFrame(content, "scroll");
Action action = new AbstractAction("scrollLastVisible") {

    @Override
    public void actionPerformed(ActionEvent e) {
        last.scrollRectToVisible(last.getBounds());
    }
};
addAction(frame, action);
show(frame, frame.getPreferredSize().width / 2, frame.getPreferredSize().height / 2);
like image 108
kleopatra Avatar answered Sep 21 '22 16:09

kleopatra