0

i want to Use an image as Jbutton back ground Using HTML with the code bellow :

JButton b = new JButton("<html><body><img src=\"file:///C|/Users/Joe/workspace/APPLET/img/2L.jpg \"></body></html>");

but the image that shows up in the button only displayed in half of the button , what should i do to fix this problem ?

  • 2
    Link to the image and to a screenshot of the button. I suspect the image is too large for the natural size of the button and you should use less image or style more space to display it. On other matters. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 14 '12 at 18:09
  • the image size is 40*40 and the button size is the same . –  Oct 14 '12 at 18:16
  • here is the picture : www.dltube.ir/jup/Jbutton.jpg –  Oct 14 '12 at 18:16
  • *"the button size is the same ."* It obviously won't paint the entire picture then, unless you managed to remove all the extra space assigned to the button decorations, insets etc. Where are those links & SSCCE? Also, why 'set the image as BG' instead of just setting the image as the icon? What is supposed to be in the 'foreground' of the button? – Andrew Thompson Oct 14 '12 at 18:17
  • excuse me body,i don't know about SSCCE but the picture of the button and frame is mentioned in the previous comment , by the way , is there any way that button size stay 40*40 and the picture with that same area cover the button surface ? –  Oct 14 '12 at 18:22
  • *"i don't know about SSCCE"* That is why my first mention of it was a link to the article. I'll read the rest of your comment after you've read the article. – Andrew Thompson Oct 14 '12 at 18:24
  • 1
    See also [this answer](http://stackoverflow.com/a/10862262/418556) for an example of using an icon (or in this case, icons) in buttons that have all the extra space removed. – Andrew Thompson Oct 14 '12 at 18:26

1 Answers1

0

What Andrew is trying to tell, amongst a number of things, is you are not taking into account the border and other possible bounding contentions which add to the size of the button...

enter image description here

The button on the left is sized to match the image size, the button on the right takes into consideration the components Insests , allowing the content to be fit within the button without effecting the borders.

Also, I don't think html is the best choice for what you want to achieve.

UPDATED

When this blows up in your face, I didn't tell you ;)

enter image description here

URL url = getClass().getResource("/DukeWaveShadow48x48.png");
String text = "<html><table  border='0' width='48' height='48' cellpadding='0' cellspacing='1' background='" + url.toExternalForm() + "'><tr><td width='46' height='46' align='center'>Hello</td></tr></table></html>";
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366