How to scroll to a specific location in a JScrollPane

痞子三分冷 提交于 2019-12-12 10:37:30

问题


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.


回答1:


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);



回答2:


It's doable.

You will need a reference to the JLabel in the header and where it links to in your view (JPanel). Once you have this link, you can need to determine the location of the JLabel within the 'JPanel'.

You can the use the JViewport.scrollRectToVisible(Rectangle) method to scroll to that location.

JLabel labelInPane = //... reference lookup
Rectangle bounds = labelInPane.getBounds();
// You may need to convert the point to meet the requirements of the parent container...
// bounds.setLocation(SwingUtilities.convertPoint(labelInPane, bounds.getPoint(), topLevelParentPaneInScrollPane));

scrollPane.getViewport().scrollRectToVisible(bounds);



回答3:


scrollRectToVisible dont't works for me. But, I solved this way:

AbstractNode a = pkmap.get(0);
scroll.getVerticalScrollBar().setValue(a.getY());
scroll.getHorizontalScrollBar().setValue(a.getX());


来源:https://stackoverflow.com/questions/12102620/how-to-scroll-to-a-specific-location-in-a-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!