Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get component from a JScrollPane

Tags:

java

swing

If there is a JEditorPane in a JScrollPane, how can you get the editor from the scrollpane?

I tried scrollPane.getComponents() but the editor wasn't in the list.

like image 703
Timotheus Avatar asked Apr 23 '11 13:04

Timotheus


People also ask

How do I get components from JScrollPane?

One way: JViewport viewport = scrollPane. getViewport(); Component[] components = viewport. getComponents();

Is JScrollPane horizontal scrollbar child field?

FieldScrollPanelLayout FieldIt is scrollpane's horizontal scrollbar child. It displays policy for the horizontal scrollbar. This displays the lower left corner.

Is JScrollPane a container?

A JScrollPane is a container that can hold one component.


2 Answers

JViewport viewport = scrollPane.getViewport(); 
JEditorPane editorPane = (JEditorPane)viewport.getView(); 
like image 99
camickr Avatar answered Oct 29 '22 01:10

camickr


One way:

JViewport viewport = scrollPane.getViewport();
Component[] components = viewport.getComponents();

although you could just have a class field that holds a reference to your editor pane and get it more easily that way.

Edit: as per Jeanette and Rob: the best way to get the single child component held by the viewport is with its getView() method.

My initial answer reminds me of a quote from H.L. Mencken:

"For every complex problem there is a solution that is concise, clear, simple, and wrong."

like image 30
Hovercraft Full Of Eels Avatar answered Oct 28 '22 23:10

Hovercraft Full Of Eels