Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making buttons circular

 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, im wondering if theres an easy way to make jbutton circular instead of rectangular. Suppose if i have a:

class Mybutton extends JButton{

}
im guessing i would have to override paintComponent but im not too good on the swing side of things. If anyone has any suggestions let me know.

Sincely,
Chris Dancy
 
Michael Dunn
Ranch Hand
Posts: 4632
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI - the buttons created from that how to that was linked will look like poo. My advice to creating round buttons is just use images. Create 3 images for each button (unpressed, pressed, and disabled) setting each for each state of the JButton. Remove the JButton's default border and you got yourself a whatever shape you want button.

Yes, this takes a bit of creative know how in an image editor, but I still think it's easier than doing it with a custom paint() method.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the round button looks OK if you change paintComponent() to this

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, I'll have to give that a shot and see what it looks like. I've tried in the past to make the buttons look better by drawing them myself but I am not very good with the graphics API. Which is why I think using images is easier.

I wonder if there is any performance difference in rendering it with graphics or using pre-made images. Anyone have any stats on that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic