JEditorPane inside JScrollPane not resizing as needed
Asked Answered
R

3

5

I am implementing a Comment box facility in my application which user can resize using mouse. This comment box contains a scrollpane which instead contains a JEditorPane in which user can insert comment. I have added the editor pane inside a scroll pane for the following reason:

auto scolling of jeditorpane

When the user resizes the comment box, I am setting the desired size for JScrollPane and the JEditorPane. When the user is increasing the size of the comment box, the size of these components are increasing as desired but when the size of the comment box is decreased, the size of the JEditorPane does not decrease even after setting the size. This leads to the scrollbars inside the scrollpane.

I tried using setPreferrredSize, setSize, setMaximumSize for JEditorPane. Still the size of the editor pane is not reducing. I tried calling revalidate() or updateUI() after the setting of size but no use.

I am using Java 1.4.2.

Please provide me some insight....

Ravens answered 11/11, 2008 at 5:35 Comment(0)
A
9

I realise this is long since answered, but for future reference all you need to do is override the getScrollableTracksViewportWidth() to always return true, eg.

JEditorPane pane = new JEditorPane() {
    public boolean getScrollableTracksViewportWidth() {
        return true;
    }
};
panel.add(new JScrollPane(pane));
Autoradiograph answered 25/10, 2011 at 12:59 Comment(1)
That's so much cleaner that writing listeners! Thumbs up from me.Pugliese
A
4

Actually it is possible, luiscubal. Here is how,

To the JScrollPane add a ComponentListener for resize events

public static void main(String...args) {
    //our test frame
    JFrame frame = new JFrame("JEditorPane inside JScrollPane resizing");
    frame.setLayout(new BorderLayout());

    //our editing pane
    final JEditorPane editor = new JEditorPane();

    //our simple scroll pane
    final JScrollPane scroller = new JScrollPane(editor);

    //NOTE: this is the magic that is kind of a workaround
            // you can also implement your own type of JScrollPane
            // using the JScrollBar and a JViewport which is the 
            // preferred method of doing something like this the 
            // other option is to create a JEditorPane subclass that
            // implements the Scrollable interface.
    scroller.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            editor.setSize(new Dimension(
                               scroller.getWidth()-20, 
                               scroller.getHeight()-20));
        }
    });

    //just use up the entire frame area.
    frame.add(scroller, BorderLayout.CENTER);

    //quick and dirty close event handler
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(320, 240); //something not too big
    frame.setLocationRelativeTo(null); //centers window on screen
    frame.setVisible(true); // normally done in a SwingUtilities.invokeLater
}

Look luiscubal it is possible. Don't be so quick to announce things in Java as not possible. The swing api is quiet flexible and can do a lot of the work for you. However, if you use JComponents in ways they weren't made to be used you will end up with problems and have two options.

  1. subclass subclass subclass basically create your own component.
  2. find a work around, like the above solution.
Anticlockwise answered 11/11, 2008 at 5:35 Comment(2)
JEditorPane already implements Scrollable. See JEditorPane.Selfconfidence
how could this have gotten any upvotes? It's plain wrong ... (though working, astonishingly ;-)Donelson
S
-1

Decreasing the size of a JEditorPane in a JScrollPane and then reducing it, is not possible. You may want to use a JTextArea instead.

Symmetrize answered 11/11, 2008 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.