How to scroll to a specific location in JScrollPane

I have a JScrollPane that contains a high-height JPanel. This large JPanel contains more Jpanels in it, as in the image. Some of these panels contain JLabel, which I used to display titles. At the top are JLabels, which have numbers corresponding to the heading numbers in the heading labels. What I need to do is when I click on the label from the list of the top label, which the JScrollBar should scroll to the position where this label is located.

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

enter image description here

+6
source share
3 answers

Assuming you want the entire panel containing the label as the title to be visible:

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

There is no need to access the containing / scrollPane viewport except (there is always an exception, right :-)

  • the component called by scrollRectToVisible has its own implementation (as fi text components
  • if the default location reached by this method does not meet your needs

Edit

code snippet for @MadProgrammer :-) - but too lazy to remove all traces 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); 
+9
source

It is doable.

You will need a link to JLabel in the header and to which it refers in your view ( JPanel ). Once you have this link, you may need to locate the JLabel inside the "JPanel".

You can use the JViewport.scrollRectToVisible(Rectangle) method to scroll to this 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); 
+6
source

scrollRectToVisible does not work for me. But I decided like this:

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

Source: https://habr.com/ru/post/923613/


All Articles