Search code examples
javajeditorpane

Add Scroll bar in JEditorPane, setLayout null


I can't add scroll bar on EditorPane.

private JEditorPane editorPane;
private JScrollPane scrollpane;

Container :

Container c = getContentPane();
    c.setLayout(null);
    setBounds(100, 100, 450, 300);

    editorPane = new JEditorPane();
    editorPane.setBounds(0, 54, 434, 208);

    scrollpane = new JScrollPane(editorPane);
    scrollpane.setPreferredSize(new Dimension(350, 110));

    c.add(scrollpane);

.. .. Nothing added


Solution

  • You're shooting yourself in the foot here:

    editorPane.setBounds(0, 54, 434, 208);
    

    By setting the editorPane's absolute size, you prevent it from expanding when it needs to do so, preventing the JScrollBars from having to show.

    Solution: don't do this. And yeah, avoid using null layouts. They'll bite you, as you're finding out. Set the width using CSS