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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:44:03+00:00 2026-06-13T13:44:03+00:00

I have a JDialog and I want to have it a certain, given size:

  • 0

I have a JDialog and I want to have it a certain, given size:

JDialog dialog = new JDialog();
dialog.setSize(800, 600);
dialog.setResizable(false);

Then I add a component:

JLabel label = new JLabel("Test");
dialog.add(label);

Now I could make the dialog visible and check the size of the component

dialog.setVisible(true);
System.out.println(label.getSize());

The answer would be “[width=784,height=562]”. Obviously the component was resized to fill the whole client area / content pane of the dialog window. That’s fine and as I want it.

Question: How can I obtain the final size of the components before calling setVisible(true)?

  • getPreferredSize() will not be the size I want because I want the component to adapt to the given dialog’s size
  • dialog.pack() is also not the right thing because it resizes the dialog to the preferred size of the components which I don’t want
  • dialog.validate() does nothing useful here, the size of the component is still 0
  • dialog.getLayout().layoutContainer(dialog) also does not set the size of the components

So I am at a loss here. I want to make the layoutmanager calculating the right sizes of all components and sub components before showing the dialog, adapted to the overall size of the dialog. But I don’t know how.

  • 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-13T13:44:05+00:00Added an answer on June 13, 2026 at 1:44 pm

    I now found that it can be done as follows:

    JDialog dialog = new JDialog();
    JLabel label = new JLabel("Test");
    dialog.add(label);
    
    // pack(), setSize(), validate() in this order will
    // set sizes on all components as wished
    dialog.pack();
    dialog.setSize(800, 600);
    dialog.validate();
    
    System.out.println(label.getSize());
    

    Also here the output is “[width=784,height=562]” but the dialog is not yet visible. The important part is the combination of pack(), setSize(desiredSize) and validate() in this order. The pack() probably determines a new size of the dialog (preferred sizes of all components), that’s why here the size has to be set afterwards and the validate() is responsible for the resizing of the components. Probably setVisible(true) which arrives at the same sizes is doing internally something similar.

    It seems a bit of a waste to resize the components several times but without pack() also setSize() and validate() do not have any effect.

    I guess the other answers were based on some misunderstanding because they implicitly always assumed that you want to have the preferred size, but there are cases, e.g. if the user resizes the dialog or if the dialogs size is fixed from the beginning where you cannot attain the preferred size and some components just have to fill the available space.

    That was the layout problem here, having a given global size of the dialog and determining the size of the components as they fill the available space. LayoutManagers solve this problem quite nicely, however usually only after setVisible(true).

    I tested a bit more:

        // new dialog
        JDialog dialog = new JDialog();
    
        // new label, prints messages if resized or painted
        JLabel label = new JLabel("Test") {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                System.out.println("Component painted.");
            }
        };
        label.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                System.out.println("Resized: " + e.getComponent().getSize());
            }
        });
        dialog.add(label);        
        System.out.println("Size after new JLabel: " + label.getSize());
    
        // pack dialog - necessary for setSize/validate to work
        dialog.pack();
        System.out.println("Size after pack: " + label.getSize());                
    
        // set a size and validate changes sizes
        dialog.setSize(800, 600);
        dialog.validate();
        System.out.println("Size after setSize and validate: " + label.getSize());        
    
        // set visible would have also done the trick
        dialog.setVisible(true);
        System.out.println("Size after setVisible(true): " + label.getSize());
    
        // and another resizing (no validation neccessary)
        dialog.setSize(300, 200);
    
        // dispose
        dialog.dispose();
    

    And the output is

    • Size after new JLabel:java.awt.Dimension[width=0,height=0]
    • Size after pack: java.awt.Dimension[width=116,height=16]
    • Size after setSize and validate: java.awt.Dimension[width=784,height=562]
    • Size after setVisible(true): java.awt.Dimension[width=784,height=562]
    • Resized: java.awt.Dimension[width=284,height=162]
    • Resized: java.awt.Dimension[width=284,height=162]
    • Component painted.

    I learned more about the inner workings of Swing:

    • ComponentResized events are not fired before setVisible(true) even if components are resized (their size changes)
    • ComponentResized events even with the same size can be fired several times in a row
    • Components might not be painted in between resizing if they follow each other fast enough
    • The first painting is in any case after setVisible(true) and the component will have the desired size (preferred size or defined by other constraints as here) by then.
    • If for some reason you must know the size of the components before the first drawing, do it with pack(), setSize(), validate()

    I tested some more, also with maximized frames and now can combine all the results into: The first painComponent() is always with the right size and the related componentResized() event always follows afterwards, sometimes two times.However the LayoutManager must know before, otherwise the examples would not be drawn correctly. So in case one draws the background by itself, either read out the right size in every paintComponent or implement a custom layout manager or wait for the resized event and invoke repaint, so the component is drawn two times but it should work. Apllications include cases where the number of components to show depend on the size (as in my geographical map application).


    Just to complete the picture I think the flow goes like this in case a user maximized or resized a frame/dialog:

    • frame/dialog.setSize()
    • LayoutManager.layoutContainer(frame/dialog) using the actual size
    • frame/dialog paint() using the layouted sizes
    • Resized() events fired for all components etc.

    And pack() probably just calls setSize(layout.preferredLayoutSize()) as the first step.

    So in case depending on the size you have to add or remove components for example, it could be a good idea to override setSize() and listen there for changes. I initially was listening for Resized() events but they arrive too late for the first drawing.

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

Sidebar

Related Questions

I have a Undecorated Modal JDialog which I want to setVisible(false) when the user
I have this JDialog with a JTabbedPane: I just want to add a JLabel
I have a jdialog and want close it on confirmation after that store the
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'm currently integrating some new classes into an existing application. I have a JDialog
Well, if we have onToped JFrame this.setAlwaysOnTop(true); and then open JDialog private void colorChooseMenuItemActionPerformed(java.awt.event.ActionEvent
I have a JDialog being displayed on screen and I want to simulate its
I have a JDialog with just a few components inside it. I want to
I have a dialog box class that extends JDialog. One method in this class

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.