Question

I'm trying to get scrollbars working on my scroll pane. Originally my pane had been just a editor pane that loaded a webpage. However, I did research and found out you can't add scrollbars natively to that kind of jSwing element. So, I made a jScrollPane and added a jEditorPane to it. Everything works great except I can't figure out why there are no scroll bars appearing and the pane is completely unscrollable. Any help is greatly appreciated, thank you.

public Main() {
    initComponents();
    scrollWeb.add(editorWeb);
    editorWeb.setEditable(false);
    editorWeb.setSize(scrollWeb.getWidth(), scrollWeb.getHeight());
    try {
        editorWeb.setPage("http://www.futureretrogaming.tk/news.html");
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

}
Was it helpful?

Solution

scrollWeb.add(editorWeb);

Don't add components to the scrollpane. Intead you add components to the viewport of the scrollpane:

scrollWed.setViewportView( editorWeb );

Or, when you create the scrollpane you can use:

scrollWeb = new JScrollPane( editorWed );

OTHER TIPS

Nothing will scroll if you force the size of the component held by the JScrollPane's viewport to the same size as the JScrollPane:

editorWeb.setSize(scrollWeb.getWidth(), scrollWeb.getHeight());

Just simply don't do this since this constrains the size of the JEditorPane to be the JScollPane's size and no larger, so it will never expand. Instead let the editorWeb be a larger size based on its content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top