• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Return sublist instead of getChildren()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about the list returned by getChildren(). Is there an easy way to restrict the type of objects allowed in and out of that list, beyond just Nodes? Say I want to only allow Buttons into a list of Children and have that checked at compile time, and also have the signature of getChildren return ObservableList<Button> instead of ObservableList<Node>. How can I do that?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please go through the documentation for getChildren(). I think it simply returns the List in question. I don't think there are any facilities in that method to specify the type of element included in the List. It is a long time since I did any FX, but I think that ObservableList extends java.util.List, whose methods include those allowing you to choose a sublist by index, or to create a Stream which you can filter to produce a new List. That latter would not behave like part of the original List. I think a sublist might but I am not sure. It says in the documentation for subList() that,

. . . non-structural changes in the returned list are reflected in this list, and vice-versa. . . .

Is there a setChildren() method? What parameter does that take? If it is ObservableList<E extends Node> or ObservableList<? extends Node>, you might be able to set it to an ObservableList<Button>. Otherwise, don'know.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the parameter's type is ObservableList<Node>, an ObservableList<Button> isn't a subtype of ObservableList<Node>, and I am afraid you are stuck with the type Node and can't change it. Can you create an anonymous class and override a method or create a new method with parameter type Button?
As I said, I haven't written any FX for a long time, but this looks more and more difficult every time I think about it.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are ways to do this but it's not going to be straightforward.

For example, you could extend Group and provide an overridden version of getChildren() that returns a list implementation that only accepts Button objects and throws an UnsupportedOperationException otherwise. But you can't have another getChildren() that returns an ObservableList<Button> instead of ObservableList<Node>. You could overload it though but the implementation will be kind of ugly (it's already ugly just thinking about it, it'll probably much uglier in actual code).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic