Purva Gupta
Purva Gupta

Reputation: 115

Make a particular component visible in a Scrollable JPanel

I have a scrollable JPanel in which are added many Editor Panes (having their respective Scroll Pane) in Box layout (Vertical Axis). My problem is that on clicking a particular button I want a particular Editor Pane to gain focus and also be visible on the screen.

I am unable to make that Editor pane visible.

I tried

scrollRectToVisible(jScrollPane5.getVisibleRect()); //It did nothing !

I also tried

scroller.setViewportView(jScrollPane5); // It made the particular editor pane occupy the entire panel !

Kindly suggest what to do ?

Thanks.

Upvotes: 1

Views: 331

Answers (2)

kiheru
kiheru

Reputation: 6618

scrollRectToVisible(theWantedEditorPane.getBounds())

Upvotes: 1

camickr
camickr

Reputation: 324167

Invoke scrollRectToVisible() on the "Editor Pane" that you want to be visible in the scroll pane.

Also, after clicking on the button it will have focus so you need to use requestFocusInWindow() on the edtitor pane that you want to have focus.

You can also check out Scrolling a Form which will do this for you whenever any component in the scroll pane gets focus.

Upvotes: 4

Related Questions