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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:12:14+00:00 2026-06-11T11:12:14+00:00

My Swing-Application sets the size of the JFrame via GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() , and all Elements

  • 0

My Swing-Application sets the size of the JFrame via GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(), and all Elements are relative to this values, so basically the application adapts to the screen resolution.

Now I have a problem with my custom buttons. For now, I create three different ImageIcons (normal, rollover and pressed) and add them to the button. But naturally, if I run the application with a different resolution than 1920×1080 (which is my default), the buttons are too big.

I found this question (http://stackoverflow.com/questions/8234726/auto-resizing-jbutton-icon) and tried the solution by creating an Icon, put it on a Jlabel and add the label to the button, but nothing is displaid. Also, I do not need to resize the window.

What is the fastest and solution for this topic? An option would be to create all custom images individually for every resolution, add the x-value of the resolution at the end of the image name and make let’s say a switch case to check which image should be load, but I think this is a very expensive and time-consuming solution. Is there an efficient way to automatically size a loaded image to a certian size relative to the screen resolution?

  • 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-11T11:12:15+00:00Added an answer on June 11, 2026 at 11:12 am

    If you have an Image, you can invoke getScaledInstance to retrieve a resized image.

    If you have an ImageIcon,you can invoke getImage() on it, to retrieve an Image and then use the solution suggested above.

    getScaledInstance returns an Image which you can wrap back into an ImageIcon and set on a JButton or a JLabel.

    So, I would create my images with the highest resolution possible and then downscale them to the appropriate resolution.

    Here is a small snippet (inspired from @mKorbel solution you started from):

    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ResizeIconInButton extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
        private JButton myButton = new JButton();
    
        public ResizeIconInButton() throws MalformedURLException {
            myButton.setIcon(new ImageIcon(new ImageIcon(new URL(IMAGE_PATH)).getImage().getScaledInstance(600, 400, Image.SCALE_SMOOTH)));
            add(myButton, BorderLayout.CENTER);
            setTitle("Resize Icon In Button");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        ResizeIconInButton main = new ResizeIconInButton();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    Small note: it is not recommended to extend JFrame but I tried to stay as close as possible to the original code.

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

Sidebar

Related Questions

I have a swing application and on the JFrame's menu I want to add
I've written a Swing application. I want to set the JFrame to be centred
I have this java swing application that I intend to sell over the internet.
In Swing application, I was wondering all the resource cleanup jobs, like : network
In my java swing application am having a Jframe and Jlabel for displaying current
Running a swing application in java, and I got this exception in my program.
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 when i run it on windows i have no
I'm developing Swing application, and everything works fine usually. But I have an GUI

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.