Get component from a JScrollPane

后端 未结 2 1086
说谎
说谎 2021-01-17 09:21

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

I tried scrollPane.getComponents()

相关标签:
2条回答
  • 2021-01-17 09:45
    JViewport viewport = scrollPane.getViewport(); 
    JEditorPane editorPane = (JEditorPane)viewport.getView(); 
    
    0 讨论(0)
  • 2021-01-17 09:46

    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."

    0 讨论(0)
提交回复
热议问题