Search code examples
javaswinguser-interfacejeditorpane

Sizing a JEditorPane within a JFrame


I'm trying to add a JEditorPane to a JFrame and I'm sizing the JFrame using pack(). When I start up the program the default size does nothing and pack makes a JEditorPane with a heigh of about 10 pixels. How can I make the JEditorPane start as the size I have defined?

public class GUIManager extends JFrame {

JButton copyButton = new JButton("Copy");
JButton cutButton = new JButton("Cut");
JButton pasteButton = new JButton("Paste");
JButton selectButton = new JButton("Select All");
JButton clearButton = new JButton("Clear");
JButton searchButton = new JButton("Search");
JButton replaceButton = new JButton("Replace");

JEditorPane editPane;
JScrollPane scrollPane;

JPanel buttons = new JPanel();

public GUIManager(){

    buttons.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

    editPane = new JEditorPane(){

        public boolean getScrollableTrackViewportWidth(){

            return true;

        }

    };
    editPane.setSize(500, 500);
    scrollPane = new JScrollPane(editPane);

    buttons.add(copyButton);
    buttons.add(cutButton);
    buttons.add(pasteButton);
    buttons.add(selectButton);
    buttons.add(clearButton);
    buttons.add(searchButton);
    buttons.add(replaceButton);

    this.add(buttons, BorderLayout.NORTH);
    this.add(scrollPane, BorderLayout.CENTER);
}

}

Solution

  • Use setPreferredSize():

    editPane.setPreferredSize(new Dimension(500, 500));