Creating a type of toggle with two labels..
jonathan.giles at oracle.com
Wed Dec 4 19:03:17 PST 2013
My advice whenever I go to conferences or get asked this is that it only
makes sense to use the full Controls API when you are planning to
redistribute your control or you plan to otherwise reuse it in a number
of applications internally where the style needs to be trivially
customised via CSS.
In other words, my advice has been much along the lines of what you said
- extend Region or another layout container if you're just creating
something custom in your own application. You gain nothing additional by
extending Control in this case.
And yes, Behavior is private API - that is a pain I know. The thing is,
how behaviors are done now is just kind of smelly, and we haven't had
the bandwidth to clean it up with all the other things we've had on our
plate, which is why they remain private API. Also, there are differing
opinions on how to progress this (personally I'm quite fond of replacing
Behavior with an @Action style annotation in conjunction with input and
action maps on the Control itself - which I protoyped into a patch
attached to ).
On 5/12/2013 3:55 p.m., John Smith wrote:
> In the past (JavaFX 2.2-), I've recommended very strongly against constructing custom controls because:
> a. For many applications it's more complicated than needed (e.g. custom controls are usually best for reusable libraries but not application code).
> b. The public API and documentation to define custom controls wasn't really there.
> For (b),
> On the documentation side, there is now stuff available at https://wiki.openjdk.java.net/display/OpenJFX/Controls.
> On the public API side there is now a more complete public API to support custom controls in Java 8.
> There are lots of great open source demos of how to create controls in the openjfx source base itself.
> Many times in application code, simple composition of existing controls via layout managers will continue to be the most appropriate way to build an application. But there are times when defining a custom control makes more sense (e.g. to take advantage of the design patterns and functionality offered by the control API or to neatly define a reusable component).
> I know the Behaviour API stuff is currently still private API, but if the Behaviour API is avoided, would you now advise writing custom controls in JavaFX 8?
> If so, would it only be when you are contributing controls to a reusable library like jfxtras or controlsfx, or would you also recommend creating custom controls for use in general application code?
> -----Original Message-----
> From: openjfx-dev-bounces at openjdk.java.net [mailto:openjfx-dev-bounces at openjdk.java.net] On Behalf Of Jonathan Giles
> Sent: Wednesday, December 04, 2013 5:12 PM
> To: Pedro Duque Vieira
> Cc: OpenJFX Mailing List
> Subject: Re: Creating a type of toggle with two labels..
> It's a toss-up between minimising duplicated code or having to bend API and implementation to your will (and end up with API you don't necessarily want in the process).
> Very rarely would I suggest extending a control - only when the API matches perfectly with what you're doing, and then that really means all you're doing is a custom skin anyway. More often than not it makes more sense to follow the famous advice of preferring composition over inheritance, and when that fails just bite the bullet and roll your own using the base Control API (if you want a reusable and skinnable / stylable control) or Region (if you don't need to be skinnable).
> -- Jonathan
> On 5/12/2013 11:05 a.m., Pedro Duque Vieira wrote:
>> OK, sounds reasonable. Too bad about the code duplication that will
>> need to take place.
>> Thanks Jonathan and thanks for the link.
>> On Wed, Dec 4, 2013 at 9:39 PM, Jonathan Giles
>> <jonathan.giles at oracle.com <mailto:jonathan.giles at oracle.com>> wrote:
>> It is likely significantly easier to simply roll your own control and
>> not try to coerce ButtonBase to support two labels.
>> For what it is worth, Paru and I talked about precisely this
>> control at
>> JavaOne 2012, which you can watch on YouTube here:
>> -- Jonathan
>> On 5/12/2013 10:16 a.m., Pedro Duque Vieira wrote:
>> > Hi,
>> > I'm trying to create a new control to be used by other
>> developers. It is
>> > know as "Toggle Switch".
>> > You can see this control in android:
>> > http://ankri.de/switch-button-for-android-2-3-gingerbread/ in iOS:
>> > http://iosguides.net/ios-menus-comparison/ (search for "Toggle
>> Switch") and
>> > on Windows 8:
>> > (below
>> > "Step 2").
>> > My approach was to subclass ButtonBase which is itself a subclass of
>> > Labeled. The problem here is that Labeled only accepts one label
>> but the
>> > Toggle Switch needs two: one for the "on" state and another for
>> the "off".
>> > Any ideas on how to best approach this problem?
>> > Thanks in advance, regards,
>> Pedro Duque Vieira
More information about the openjfx-dev