Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Image as a button

 
Greenhorn
Posts: 4
Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make an image that can act like a button in java application.

I have written :



but the result is there is an image inside a button. It isn't what I want to do, I want to make an image that acts like a button...Any suggestion?...Thanks for sharing knowledge..^^
 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a JLabel instead of a JButton, and use a mouse listener. You can also change the cursor of the label if you want to.

I'll move this thread to our GUI forum.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Or you can also
1) With your existing code, make your button transparent by using JButton#setContentAreaFilled(false)
2) Refer to https://coderanch.com/t/506289/GUI/java/custom-shaped-gui-components and http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/

 
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kenji jonathan wrote:I want to make an image that can act like a button in java application.

I have written :



but the result is there is an image inside a button. It isn't what I want to do, I want to make an image that acts like a button...Any suggestion?...Thanks for sharing knowledge..^^



This is what I wrote to make a transparent button having only the icon:

 
kenji jonathan
Greenhorn
Posts: 4
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow..thanks for sharing...by the way, now I want to place the button at the center of the window. Is there any simple way to do it?....
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kenji jonathan wrote:I want to place the button at the center of the window. Is there any simple way to do it?....



If the button is the only component in its Container, set a GridBagLayout and add the button without a GridBagConstraints.
 
Rancher
Posts: 3205
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reply
    Bookmark Topic Watch Topic
  • New Topic