0

I'm making a Java Swing application and I want to use icons as a button, I used the following code to make my Icon/JLabel instances:

ImageIcon homeIcon = new ImageIcon(ImageIO.read(new File(new File(".").getCanonicalPath() + File.separator + "Icons\\home.png")));
JLabel homeButton = new JLabel( homeIcon,JLabel.CENTER);
homeButton.setBounds(15, 72, 30, 30);
homeButton.setFocusable(false);
add(homeButton);
homeButton.setOpaque(false);

But I'm getting my image as low quality. I don't think it's a scaling problem because the image is 30x30 and so does the JLabel, and resizing the label doesn't seem to change the icon's size, it's just low quality.

I looked for the other answers, mostly to no avail, however, I encountered a high-quality resizing library but I couldn't find documentation for that and I'm not sure whether it is available for commercial use or not.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    The issue is either 1) the quality of the image itself or 2) your application is being scaled. Look at your OS settings for the desktop scaling. See: https://stackoverflow.com/questions/65742162/how-to-fix-the-blurry-image-when-i-run-my-java-swing-project/65742492#65742492 for more suggestions. Also. don't use setBounds(). Swing was designed to be used with layout managers. Variable names should NOT start with an upper case character. Follow examples from your text book or tutorial. – camickr Sep 22 '21 at 14:10
  • @camickr Thanks, I think its the 2nd issue since the image looks fine on other software's and I tried multiple images to no avail. I'll try not to use set bounds, I tried it here to test its size. About variable names: thank you for the advice, this is a project I'm revisiting so there are a lot of rookie mistakes I did, I usually use camel case (like: homePage) or snake (home_page), thanks for the advice, I'll change the names right away! –  Sep 22 '21 at 14:53
  • 1
    There is no need for all those File acrobatics. Just use `new ImageIcon("Icons\\home.png")`. Or, even better, `new ImageIcon(getClass().getResource("/Icons/home.png"))`. – VGR Sep 22 '21 at 16:20
  • 1
    Can you post the image and a screenshot of the app? – Charlie Sep 22 '21 at 17:52
  • @Charlie *"Can you post the image and a screenshot of the app?"* In addition to those two, add a [mre] that hot links to the icon of interest (embed the image using an [edit] to the question then use that image URL directly in the code). I am not convinced this is not due to scaling. The icon itself and the screenshot might help reveal the facts there, but the MRE also gives us a chance to try it on our own systems. – Andrew Thompson Sep 23 '21 at 01:00
  • 1
    BTW *"use icons as a button"* Should read *" use icons **in** a button"*. Don't use a label as a button. A regular `JButton` can *appear* to be a label, but offers so much more useful stuff automatically (keyboard input, the ability to change the icon on hover, press etc., support for an `ActionListener` or `Action` ..) – Andrew Thompson Sep 23 '21 at 01:27

0 Answers0