Question

I have a JEditorPane that is inside a rectangle in a window so that it doesnt fill the whole window. However when the HTML page gets too big, it cuts it off instead of putting in a scrollbar, I know I need a JScrollpane or something, but I dont want the scrollbar on the whole window, only inside the rectangle.

Here is a snippet of my code (rweb is already defined as a Rectangle):

        JEditorPane web = new JEditorPane();
      web.setEditable(false);  
        try {
      web.setPage("http://www.google.com");
    }catch (IOException e) {
      web.setContentType("text/html");
      web.setText("<html>Could not load webpage</html>");
    } 
    web.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                if(Desktop.isDesktopSupported()) {
                    try {
                        Desktop.getDesktop().browse(e.getURL().toURI());
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (URISyntaxException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }
    });
    rweb = new Rectangle(0, 0, width, 600);
    web.setBorder(null);
    web.setBounds(rweb);
    window.add(web);
Was it helpful?

Solution

You never add the JEditorPane to a JScrollPane. Text components don't have scroll support by themselves.

Try using window.add(new JScrollPane(web)); instead

Take a look at How to use scroll panes for more details

ps- web.setBounds(rweb); will only work if you are using a null layout and if you are, you'd be better of using a EmptyBorder or a layout manager that provided the ability to specify insets, like GridBagLayout

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