Is there a way to create a JButton with your own button graphic and not just with an image inside the button?
If not, is there another way to create a custom JButton in java?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When I was first learning Java we had to make Yahtzee and I thought it would be cool to create custom Swing components and containers instead of just drawing everything on one
JPanel. The benefit of extendingSwingcomponents, of course, is to have the ability to add support for keyboard shortcuts and other accessibility features that you can’t do just by having apaint()method print a pretty picture. It may not be done the best way however, but it may be a good starting point for you.Edit 8/6 – If it wasn’t apparent from the images, each Die is a button you can click. This will move it to the
DiceContainerbelow. Looking at the source code you can see that each Die button is drawn dynamically, based on its value.Here are the basic steps:
JComponentsuper()in your constructorsMouseListenerPut this in the constructor:
Override these methods:
Override this method:
The amount of space you have to work with when drawing your button is defined by
getPreferredSize(), assuminggetMinimumSize()andgetMaximumSize()return the same value. I haven’t experimented too much with this but, depending on the layout you use for your GUI your button could look completely different.And finally, the source code. In case I missed anything.