Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 589077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:23:08+00:00 2026-05-13T15:23:08+00:00

I have a JEditorPane which is displayed inside a popup, triggered through a button.

  • 0

I have a JEditorPane which is displayed inside a popup, triggered through a button. The pane contains long text, therefore it’s nested inside a JScrollPane, and the popup is constrained to a maximal size of 300 x 100:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            String text = "Potentially looooooong text. " + 
                "Lorem ipsum dolor sit amet, consectetuer" +
                "adipiscing elit, sed diam nonummy nibh euismod " +
                "tincidunt ut laoreet dolore magna aliquam" + 
                "adipiscing elit, sed diam nonummy nibh euismod" + 
                "erat volutpat. Ut wisi enim ad minim veniam, " + 
                "quis nostrud exerci tation.";

            final JEditorPane editorPane = new JEditorPane("text/html", text);
            editorPane.setEditable(false);

            final JButton button = new JButton("Trigger Popup");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPopupMenu popup = new JPopupMenu();
                    popup.setLayout(new BorderLayout());
                    popup.add(new JScrollPane(editorPane));
                    Dimension d = popup.getPreferredSize();
                    int w = Math.min(300, d.width);
                    int h = Math.min(100, d.height);
                    popup.setPopupSize(w, h);
                    Dimension s = button.getSize();
                    popup.show(button, s.width / 2, s.height / 2);
                }
            });

            JFrame f = new JFrame("Layout Demo");
            f.setSize(200, 200);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.getContentPane().add(button);
            f.setVisible(true);
        }
    });
}

When the JEditorPane instance is shown for the first time (i.e. when the button is clicked once) it somehow seems to report a preferred height which is too small (1):

first invocation

After subsequent button clicks, the layout is just how one would expect it (2):

subsequent invocations

How can I enforce/impose a proper preferred size so it would always initialize like (2)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:23:09+00:00Added an answer on May 13, 2026 at 3:23 pm

    The JEditorPane cannot compute its final preferred width and height simultaneously, it has to know one before it can compute the other.

    On the first pass, the JEditorPane computes its preferred height based on the assumption that its width will be unlimited, so it returns the height of a single line (since the text contains no line breaks.)

    On the second pass, the width has already been set (constrained by the size of the first JPopupMenu), and now that it knows the maximum width it can compute how tall it needs to be.

    So the simplest solution is just to set the width to the maximum whenever you set the text.

    String text = "Potentially looooooong text. " + 
        "Lorem ipsum dolor sit amet, consectetuer" +
        "adipiscing elit, sed diam nonummy nibh euismod " +
        "tincidunt ut laoreet dolore magna aliquam" + 
        "adipiscing elit, sed diam nonummy nibh euismod" + 
        "erat volutpat. Ut wisi enim ad minim veniam, " + 
        "quis nostrud exerci tation.";
    
    final JEditorPane editorPane = new JEditorPane("text/html", text);
    editorPane.setSize(300, Integer.MAX_VALUE);
    editorPane.setEditable(false);
    

    Don’t worry about making it too large, it will still shrink to fit the content (as you will see if you change the text to "Hello, World!".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 295k
  • Answers 295k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MySQL, which you appear to be using, does not support… May 13, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer contentsOfDirectoryAtPath:error: returns an NSArray. Just send count to the array. May 13, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer No. This can only be done using a dynamic query.… May 13, 2026 at 6:53 pm

Related Questions

I swear... i hope this is the last question I have to ask like
I have a swing application that sends commands to server and receives result in
I have model that is a queue of Strings associated with enum types. I'm
I'm writing a Java application with GUI using Swing. One of the GUI components
I'm currently trying to solve a problem where I need to find the position

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.