Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes How to create a different shape JButton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to create a different shape JButton?" Watch "How to create a different shape JButton?" New topic
Author

How to create a different shape JButton?

Jc Frane
Ranch Hand

Joined: Aug 15, 2013
Posts: 39
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

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())


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
GeeCON Prague 2014
 
subject: How to create a different shape JButton?