This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to code a button with image

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
I've never won anything before. Not even a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic