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 8574029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:23:01+00:00 2026-06-11T19:23:01+00:00

I have a JDialog with just a few components inside it. I want to

  • 0

I have a JDialog with just a few components inside it. I want to make the dialog as small as possible. Currently I am using pack(). This has the unintended effect of reducing the dialog’s width so much that the title is no longer completely in view. I want the dialog’s width to always be great enough such that the title is always completely in view.

I am using swing. I realize that the title bar appearance/font is determined by the OS. I would prefer to stick with swing so at the moment i am planning on calculating the title string width based on the font of a JLabel. Then I will set the minimum width of one of my components equal to that.

Is there any better way to pack a JDialog while keeping its title visible?

  • 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-06-11T19:23:02+00:00Added an answer on June 11, 2026 at 7:23 pm
     public static void adjustWidthForTitle(JDialog dialog)
    {
        // make sure that the dialog is not smaller than its title
        // this is not an ideal method, but I can't figure out a better one
        Font defaultFont = UIManager.getDefaults().getFont("Label.font");
        int titleStringWidth = SwingUtilities.computeStringWidth(new JLabel().getFontMetrics(defaultFont),
                dialog.getTitle());
    
        // account for titlebar button widths. (estimated)
        titleStringWidth += 110;
    
        // set minimum width
        Dimension currentPreferred = dialog.getPreferredSize();
    
        // +10 accounts for the three dots that are appended when the title is too long
        if(currentPreferred.getWidth() + 10 <= titleStringWidth)
        {
            dialog.setPreferredSize(new Dimension(titleStringWidth, (int) currentPreferred.getHeight()));
    
        }
    }
    

    EDIT:
    after reading trashgod’s post in the link, I adjusted my solution to override the getPreferredSize method. I think this way is better than my previous static method. Using the static method, I had to adjust it in a pack() sandwich. pack(),adjust(),pack(). This wasy doesn’t require special consideration with pack().

    JDialog dialog = new JDialog()
        {
            @Override
            public Dimension getPreferredSize()
            {
                Dimension retVal = super.getPreferredSize();
    
                String title = this.getTitle();
    
                if(title != null)
                {
                    Font defaultFont = UIManager.getDefaults().getFont("Label.font");
                    int titleStringWidth = SwingUtilities.computeStringWidth(new JLabel().getFontMetrics(defaultFont),
                            title);
    
                    // account for titlebar button widths. (estimated)
                    titleStringWidth += 110;
    
                    // +10 accounts for the three dots that are appended when
                    // the title is too long
                    if(retVal.getWidth() + 10 <= titleStringWidth)
                    {
                        retVal = new Dimension(titleStringWidth, (int) retVal.getHeight());
                    }
                }
                return retVal;
            }
    
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this JDialog with a JTabbedPane: I just want to add a JLabel
I have been using this code to implement a Popup JDialog of sorts, like
I have a modal settings dialog which is a JDialog. In this settings window
Hi I have only one JDialog box in my Java application.I want to make
I have a dialog box class that extends JDialog. One method in this class
I have a jdialog and want close it on confirmation after that store the
I have a Undecorated Modal JDialog which I want to setVisible(false) when the user
I have a JDialog that consists of two JPanels, one above the other. Currently,
I have a jDialog with a splitter inside it. One side has a tree
I'm currently integrating some new classes into an existing application. I have a JDialog

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.