When i try to add background in Jbutton in java by going to its properties and then icon, it then sets the background of the button but the text which i have written on the
If you wanted to add an Image to your JButton
with NetBeans
follow these steps :
Source Packages
, under Projects
and Select New -> Other -> (Under Categories) Select Other -> (Under File Types) Select Folder
.resources
, do check that for Parent Folder
, src is written inside the field. Now Click Finish.IMAGE
inside this folder.Design Mode
, select your JButton, and on the Right Side go to this JButton
's properties. Just under foreground you will see icon
is written, click the Eclipse Button associated with it to open the window as shown in Figure below : You are done adding image to your JButton. If you want to use the image I used, here it is . Yeah, I forgot to mention, for this, I had set horizontalTextPosition = CENTER and veritcalTextPosition = BOTTOM under Other Properties, inside Properties.
And Here is the output of the whole thing :
What is the solution for this?
One 'solution'1 it to write the text on the image. This is not a very good solution though, since:
As far as I understand you are not setting background but you are setting Icon to button by this. And that is why you see Image on right side and text on left side. To add image as background to button you need to override paintComponent(g)
method of JButon
and draw your image in it.