• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to code a button with image

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I want to have a button with image background,but i don't want to use Swing.
How to?
Thanks your answer in advance.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rose,
Basically, all you have to do is extend Component, Button, Panel, Canvas... any of the AWT types you could use to draw an image on... you would send the image into your new class via the constructor, or a set method, and have the new component paint the image in it's paint() method... you would need to make it create ActionEvents( since that's what buttons create ) when it is clicked on... you would probably do this by making the new class implement MouseListener and KeyListener( to get mouse clicks and key presses on this new class ) and have it draw the new class differently( i.e. different image, border change, something to show it is "pressed" ) and have it create the ActionEvent...
Then you just use this new AWT component in your applet or application.
I know that I was kind of sketchy on the details... please ask if something is not understandable, or if you need an example.
HTH,
-Nate
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rose,
Here is an example of some code to create a button with an image on it... if it meets your needs, great! If not, hopefully it can be modified to do just what you want!

Just use it like a normal Button in your applets or applications!
Email me or post on here if you have any problems with it!
HTH,
-Nate
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic