I’ve been trying to implement a completely free bordered JButton :
- Free shape as a border, as a solution is given in a previous thread : Rounded Swing JButton using Java
- But the shape outside area can’t be clicked
- And the shape outside area is not drawn, even by the parent class (JButton)
Is there a simple way to do it in “plain” Swing (without any 3d party library) ? Otherwise, is there a good free (open-source is better of course) 3d party library in order to do this ?
As an example, think about a Red Circle ball : the best interaction I can give to users is that it can’t click in the outside area, particularly if the Ball is really big.
Thank you in advance
(I’ve thought of a component written from scratch with Java2D, and using the Shape contains() method in order to see if the click is good. But I would have 2 main problems :
-
for giving the pressed apperance to the Shape, as well as the JButton does it => as someone noticed me below, that’s not the most difficult, if I use two differents drawings/images
-
for handling events (ActionListeners) as well as the JButton does it => if i inherits from a JComponent, the most difficult will be to reject (return from) the handling if we have not clicked on the good area => what is the best way to detect this eventual intersection between mouse clicking point and the area ?)
you have three choises
1) every Custom Look and Feels have implements
Rounded JComponents, this is safiest way how to do that confortly and maybe correctly too, part of them brings excelent output to theJava Swing GUIand with longBughistory, I’d suggest to useSubstanceLook & Feel, but is very, so sensitive to EDT2) you can override whole (Custom
Look and Feelsalready did that) ButtonUI, but yourUI_Delegatewill beLook and FeelandNative_OSsensitive, meaning example that link I post, works onWindowsplatform withMetal Look and Feel, if you want to create an crossPlatformand crossLook & Feelnon-sensitive, then you must to overrive all possiblePlatformsandLook and Feels3) create
JComponentwithRounded Borders, fill area inside with for exampleGradientPaint, and then addMouseListener,MouseMotionListener,KeyListeneror better write ownKeyBindingsand create ownModelfor avoiding the concurency betweens linsteningListeners, then will be yourCustom JButtoncrossPlatformand crossLook & Feelnon-sensitiveeverything …. up to you, much luck