Buttons in shapes other than rectangles

Is it possible to create buttons in shapes other than rectangles (or rounded rectangles). Could you, for example, create a button in the form of a state on a map? Will clicking anywhere on the form cause the MouseEvent to be broadcast?

0


source to share


3 answers


You can create buttons like this by extending the JButton and calling setContentAreaFilled(false);

 in the constructor. Then you can draw whatever you like in the overridden method paintComponent()

. You also need to override the method contains()

JComponent

to provide the required mouse hit test. In this method, you can call the polygon (the one used to draw the button) contains()

.



+2


source


In Java, render your map using instances of classes that implement the interface Shape

, eg Polygon

. Any of the supported methods contains()

will work.



+2


source


Here's an example from JDC's technical tips:

http://web.mit.edu/java/JDCNewsletter/JDC-TECH8-99

+2


source







All Articles