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

  • Home
  • SEARCH
  • 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 8569101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:15:25+00:00 2026-06-11T18:15:25+00:00

I have a swing component where the ideal size of the component will vary

  • 0

I have a swing component where the ideal size of the component will vary based on the content that is is currently being displayed (which happens in this case to be an image).

I’d like this preferred size to change when the content is changed, and also allow the layout to be changed (for example, if the component is being used inside a JScrollPane then the scroll extents would change to fit the size of the component).

What is the canonical way to do this in Swing ?

  • 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-11T18:15:27+00:00Added an answer on June 11, 2026 at 6:15 pm

    Suggestions:

    • Use a class extends JPanel (or JComponent),
    • give it a getPreferredSize() method override where you return a Dimension with the parameters that you desire.
    • For instance if the diameter will be based on a BufferedImage, you could have something like:

    getPreferredSize example:

    public Dimension getPreferredSize() {
      if (myBuffImg != null) {
        return new Dimension(myBuffImg.getWidth(), myBuffImg.getHeight());
      } 
      // default return value
      return super.getPreferredSize();      
    }
    

    Edit
    Regarding your comment:

    how would you handle the case of the component’s content image changing though? is it just a a case of triggering a re-layout in the surrounding container?

    You’d give this class a setImage(Image image) method of course, and you could repaint() this panel from within this method. The method I suppose could call revalidate() on this JPanel’s ancestor causing this component to be re-layed out, but I’m not too crazy about methods in this class having side effects on its ancestor and think that likely it will be better for the code calling the setImage(...) method to suggest that the container revalidate itself.

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

Sidebar

Related Questions

I have a test that exercises a custom Swing component using java.awt.Robot. I'd like
I have a swing component which draws a fixed large (but fixed) vector image
I'm working on a Swing based project that will display large amounts of data
I have a custom swing component that is implemented similar to a JTree. It
I have an AWT canvas which I cannot convert to a Swing component (it
I have a custom Swing component called NavigationLink which extends JLabel and implements a
Say I have a standard Swing component like JSlider, but I want to slightly
I have a Swing application that connect directly to MySql Database for user authentication.
I have a swing application that uses some datas coming from an external mysql
i have a swing application that i would like to run on os x

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.