0

I am creating a Java application and use Swing for the UI. I would like to add some JButtons with icons instead of text. However, when I set the icon on the button, the icon becomes very ugly.

Here is the icon I use:
enter image description here

The icon is 20x20, and the JButton I place the icon on is also 20x20, but on the JButton the icon looks like this:

enter image description here (Screenshot from Netbeans IDE)

How do I fix this?

Here is the code (just copied from Netbeans):

package com.mycompany.mavenproject2;
public class Test extends javax.swing.JFrame {

    /** Creates new form Test*/
    public Test() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jButton1.setIcon(new javax.swing.ImageIcon(path_to_file)); // NOI18N
        jButton1.setMaximumSize(new java.awt.Dimension(20, 20));
        jButton1.setMinimumSize(new java.awt.Dimension(20, 20));
        jButton1.setPreferredSize(new java.awt.Dimension(20, 20));
        getContentPane().add(jButton1);

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   

}
Christian O.
  • 468
  • 2
  • 12
  • 1
    `jButton1.set..Size(new java.awt.Dimension(20, 20));` If it's a 20x20 icon and is shown in a button with borders, the button needs to larger than 20x20. More generally: See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) **General Tip:** One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an .. – Andrew Thompson Jan 03 '21 at 15:21
  • .. image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jan 03 '21 at 15:21
  • 1
    You are probably using Windows scaling so the Icon is upscaled. See: https://stackoverflow.com/questions/64586078/java-swing-jbutton-jlabel-icons-are-not-displayed-in-their-original-size for a couple of solutions. – camickr Jan 03 '21 at 15:36
  • @camickr That was the problem, thank you very much! You can write an answer and I'll accept it or flag the question as duplicate. Also, a question since you are the author of the answer in the question you linked: You wrapper class fixes the scaling problem but introduces the problem that the button will only display the image once I hover over it for the first time. Any idea what could be the cause of that? – Christian O. Jan 03 '21 at 15:53
  • Did you test the code as posted? The Icon displays properly for me, so I don't know why it would be different for you. – camickr Jan 03 '21 at 16:19

0 Answers0