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