Setting Image on a Button using Netbeans 8.2

Asked

Viewed 212 times

0

I am trying to insert an image . png in a jbutton, I put the image in a package and I am searching them with:

        btnCalcBanco.setBackground(new java.awt.Color(255, 255, 255));
        btnCalcBanco.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Calculadora.png"))); // NOI18N
        btnCalcBanco.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 153, 255)), "Calculadora", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.BELOW_TOP));
        btnCalcBanco.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCalcBanco.setMaximumSize(new java.awt.Dimension(201, 131));
        btnCalcBanco.setMinimumSize(new java.awt.Dimension(201, 131));
        btnCalcBanco.setPreferredSize(new java.awt.Dimension(20, 13));
        btnCalcBanco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcBancoActionPerformed(evt);
            }
        });

This code is generated by NETBEANS, when I had the image imported into the package, I already checked the image path and it’s right and even appears in the window like this:

inserir a descrição da imagem aqui

but when I have the project run it error in the execution this error:

inserir a descrição da imagem aqui

1 answer

0

I don’t know if your image is inside the classpath of your application. Apparently it is, since you can see its preview in the IDE.

Can make a test and check if instantiating like this works?

  JButton button = new JButton();
  try {
    Image img = ImageIO.read(getClass().getResource("/resources/calculadora.png"));
    button.setIcon(new ImageIcon(img));
  } catch (Exception ex) {
    System.out.println(ex);
  }

This code assumes that the image will be in the folder /Resources/

  • It’s returning this here java.lang.Illegalargumentexception: input == null!

  • This problem does not seem to me related to the image, can put a little more of your code and stacktrace editing the question?

  • I just noticed a problem with my answer and edited, the expected image path needs a "/" at the beginning, and you can see why here: https://stackoverflow.com/questions/14739550/difference-between-getclass-getclassloader-getresource-and-getclass-getres

  • how so not understood, where I have to put the "/"

  • It was only the first bar within the getResource() argument. I just created a similar code here and apparently the solution worked without exceptions. You can run a clean/build of your project if you haven’t already?

  • I got it! the problem was that I was using a preview of the "java" folder but the netbeans was searching in the "Resource" folder and it was showing me, but at the time of running it could not find the image

  • Igor, you shouldn’t leave questions "open" especially when the problem has been solved. You should approve a response or submit your own response and approve it

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.