• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JavaFX ComboBox binding to value

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a combo box which it's values are bi-directionally bound to values in the ViewModel.
I think I am close but can't get the value of this.accountRisk set to one of the selectable values.  
The bolded code below is what is not working.  Any simplifications to this code would also be appreciated.  



Thanks


 
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post a more complete example (something that can be copy/pasted that compiles is most helpful)?

Alternatively, what's the error or what isn't working?


To take a guess looking at this pseudocode, it looks like you're trying to set a String on a property that expects a SingleSelectionModel.

accountRisk.get().select(risks.get(2));

This should set the value on your model.

Also I would avoid bidirectional bindings if you don't really need them (one directional is just bind()).
 
Lou Hamers
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, you'd have to set a value for accountRisk first though. Otherwise you'll just get an NPE (NullPointerException) from the get(), obviously.
 
Jeff Dickson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a runnable simplified example.  I know that risks.get(2) is an incorrect assignment.  I put it there to show what is desired, but I think it made it more confusing.

The bidirectional binding of the list of values is working correctly, but I also wanted a binding for the selected value.  

If "accountRisk.selectionModelProperty()..." is uncommented, it seems to be competing with the user selected value.  

I just wanted the binding to set the initial value and then allow the user to change it which then bidirectionally sets the selected value back in the ViewModel.

Sorry for the confusion, it was late on Sunday when I gave up trying to make this work.
 
 
Lou Hamers
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's some other things we could discuss here but this does it - I did rename one of your accountRisks to accountRisksModel.
I think maybe you were confusing those two items?

 
Lou Hamers
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally I'd add a little more info and cleanup but I'm exhausted tonight. Hopefully this will get you going again anyway.

You could also use select(risks.get(2)) like you were trying to use before.
 
Jeff Dickson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Lou.  

I can't pass the ComboBox's selection model into the view model, so I am trying to set it via a method in the view model.
The view model is injected into all the controllers using DI and is created two levels up.

In the process of renaming a bunch of stuff and incorporating the change into my project, I have created a circular reference or an incorrect order of initialization.

Time to wrap it up for the night, it has been a very long day.

I will work on it again tomorrow night.

Thanks again for the key bit of information.
 
Jeff Dickson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My last problem seems to be related to the groovy compiler.  Groovy was happy with the syntax while the Java compiler was not.  See code comments below.

 
Lou Hamers
Bartender
Posts: 303
12
IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>// Groovy is happy with this while Java is not.

I can't speak too much on Groovy - not sure what black magic it's doing there.

But in Java World calling set() on SimpleObjectProperty returns void, so it needs to be two lines. It's more readable that way, anyway, IMO.

 
There's a way to do it better - find it. -Edison. A better tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic