AuBee
AuBee

Reputation: 86

How to resize edit area in JEditorPane?

This is a question about Java and Swing.

I put two JEditorPane into two JScrollPane, and then put the two JScrollPane into a JSplitPane. The problem is that when I drag to resize the JScrollPane, one JEditorPane is extended, but when I drag back, the view shrinks but the edit area doesn't, the horizontal scroll bar appeared.

The code looks like this:

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;

public class Main {

    private static final int DEFAULT_WIDTH = 800;
    private static final int DEFAULT_HEIGHT = 600;

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

                JEditorPane editor = new JEditorPane();
                JEditorPane preview = new JEditorPane();

                System.out.println(DEFAULT_WIDTH / 2);
                // editor.setSize(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT);
                editor.setMinimumSize(new Dimension(0, 0));
                // preview.setSize(DEFAULT_WIDTH / 2, DEFAULT_HEIGHT);
                preview.setMinimumSize(new Dimension(0, 0));

                final JScrollPane scrollEditor = new JScrollPane(editor);
                final JScrollPane scrollPreview =  new JScrollPane(preview);

                final JSplitPane innerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollEditor, scrollPreview);
                innerPane.setContinuousLayout(false);
                frame.add(innerPane, BorderLayout.CENTER);

                innerPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        editor.setSize(innerPane.getLeftComponent().getBounds().width, editor.getHeight());
                        scrollEditor.setSize(innerPane.getLeftComponent().getBounds().width, editor.getHeight());
                    }
                });
                frame.setVisible(true);
            }
        });
    }
}

I try to use JEditorPane.setSize, but it doesn't work.

How can I fix it?

Thanks.

Upvotes: 0

Views: 778

Answers (2)

Lalith J.
Lalith J.

Reputation: 1391

final JScrollPane scrollEditor = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
final JSplitPane innerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, scrollEditor, scrollPreview);

innerPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {

                        editor.setSize(innerPane.getLeftComponent().getBounds().width, editor.getHeight() - 10); // If you want more edge space make 10 to 15 or 20
                        scrollEditor.setSize(innerPane.getLeftComponent().getBounds().width, editor.getHeight());

                    }
                });

Upvotes: 0

camickr
camickr

Reputation: 324147

You need to override the default implementation of the Scrollable interface for your JEditorPane. Basically you need to tell the JScrollPane that the width of the JEditorPane should always fit inside the viewport of the scrollpane.

You do this by using code like:

JEditorPane editor = new JEditorPane()
{
    @Override
    public boolean getScrollableTracksViewportWidth()
    {
        return true;
    }
};

And as I mentioned in my comment you don't need the PropertyChangeListener.

Upvotes: 2

Related Questions