• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a different shape JButton?

 
Jc Frane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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())
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic