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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:17+00:00 2026-06-13T23:08:17+00:00

I have this little code public class Test extends JFrame { public static void

  • 0

I have this little code

public class Test extends JFrame {

public static void main(String[] args) {
    new Test();
}

Test() {
    getContentPane().add(new MyPanel());

    pack();
    setVisible(true);
}

private class MyPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        setSize(640, 480);
        setPreferredSize(new Dimension(640, 480));
        setMinimumSize(new Dimension(640, 480));
        g.fillRect(20, 20, 50, 50);
    }

}

Unfortunately the frame is not resized to the size of the nested panel after calling pack(). I already read the related answers for this topic, but none of them helped. Could you provide me a solution, please?

  • 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-13T23:08:17+00:00Added an answer on June 13, 2026 at 11:08 pm

    When pack() is executed the panel is still invisible and its paintComponent() was not executed and as a result setPreferredSize() wasnt executed as well.

    But don’t call setPreferredSize from paintComponent(). Do your painting in paintComponent nothing else. Avoid putting program logic into that method. Painting operations should be fast and optimized for better performance and user experience. See Performing Custom Painting for more details.

    Override panel’s getPrefferedSize(), or at least execute setPrefferedSize before pack().

    Also see Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing.

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

Sidebar

Related Questions

I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
I have code a bit like this public class MyObject { private bool IsValidDay(ref
I have taken this little snippet straight out of some code I'm working on:
its a little bit hard to understand. in the header.php i have this code:
I have this little rake task: namespace :db do namespace :test do task :reset
In my little code snippet below I have a wrapper class for a simple
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web

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.