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:
but when I have the project run it error in the execution this error:
It’s returning this here java.lang.Illegalargumentexception: input == null!
– Igor Paslauski
This problem does not seem to me related to the image, can put a little more of your code and stacktrace editing the question?
– fwerther
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
– fwerther
how so not understood, where I have to put the "/"
– Igor Paslauski
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?
– fwerther
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 Paslauski
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
– fwerther