• 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
  • Ron McLeod
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

How to create a different shape JButton?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner in java programming, I almost try all examples on net in creating Java Components, and it is pretty well. Now my teacher wants me to work on a project. I am sick of the look of JButton.

My question is: "Is it possible for me to change the JButton's shape?"

I've heard of something like overriding paintComponent() method and extending JButton. However, I am not sure how to do it.

Another thing I read is that I can use JPanel and add some mouseListener to it. I tried to draw a .png file using photoshop and add it to my panel, but the thing is the panel's background remains
intact (the rectangle border). How can I remove that if I want my button to be for example circle?

Please send a simple code regarding this.

Thanks in advance.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Custom painting could be one of the possible solutions. More on it here http://docs.oracle.com/javase/tutorial/uiswing/painting/
Another approach could be to provide an image icon to the button and make it borderless and transparent so that only the image shows. Of course you need to consider that the button is still a clickable rectangle. In other words if your image is a red circle at the center of the button the corners of the original button would still exist.
This is similar to the panel approach you mentioned. To make things transparent use setOpaque (or in case of button setContentAreaFilled())
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jc Frane wrote:I am a beginner in java programming, I almost try all examples on net in creating Java Components, and it is pretty well. Now my teacher wants me to work on a project. I am sick of the look of JButton.


If you're crazy about fancy effects, I suggest you abandon Swing in favor of JavaFX.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic