Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » E4 DI in "onAction" methods(How can I use E4 DI in methods triggered by UI events?)
E4 DI in "onAction" methods [message #1384676] Fri, 30 May 2014 09:20 Go to next message
Uwe San is currently offline Uwe SanFriend
Messages: 119
Registered: January 2012
Senior Member
Hi,

I'm looking for a good solution for the following problem:

Asssume you have an FXML file with a combo box and an associated onAction() method like
<ComboBox ... onAction="#handleSelection" />

The controller's handleSelection() method uses an instance of type Foo that was injected by E4 into the controller during its instantiation.

Now assume you cannot guarantee there is a Foo instance available at instantiation time, so you would like to inject it into the handleSelection() method, as this is the point in time where you actually need the instance:
@FXML
@Inject
public void handleSelection(Foo foo) {
...
}

As you might expect, this code cannot be parsed by the FXML loader:
javafx.fxml.LoadException: Error resolving onAction='#handleSelection', either the event handler is not in the Namespace or there is an error in the script. 


What would be a good solution for this problem at the interface of JavaFX and E4? Of course, you could use ContextInjectionFactory.inject() or maybe call an E4 command programmatically, but these approaches don't seem very elegant.

Thanks,
Uwe
Re: E4 DI in &quot;onAction&quot; methods [message #1384680 is a reply to message #1384676] Fri, 30 May 2014 10:20 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Well you could declare your stuff as:

private Foo foo;

@Optional
@Inject
public void setFoo(Foo foo) {
this.foo = foo;
}

@FXML
public void handleSelection() {
if(foo != null) {
// ....
}
}


On 30.05.14 10:20, Uwe San wrote:
> Hi,
>
> I'm looking for a good solution for the following problem:
>
> Asssume you have an FXML file with a combo box and an associated
> onAction() method like <ComboBox ... onAction="#handleSelection" />
> The controller's handleSelection() method uses an instance of type Foo
> that was injected by E4 into the controller during its instantiation.
>
> Now assume you cannot guarantee there is a Foo instance available at
> instantiation time, so you would like to inject it into the
> handleSelection() method, as this is the point in time where you
> actually need the instance:
>
> @FXML
> @Inject
> public void handleSelection(Foo foo) {
> ..
> }
>
> As you might expect, this code cannot be parsed by the FXML loader:
> javafx.fxml.LoadException: Error resolving onAction='#handleSelection',
> either the event handler is not in the Namespace or there is an error in
> the script.
> What would be a good solution for this problem at the interface of
> JavaFX and E4? Of course, you could use ContextInjectionFactory.inject()
> or maybe call an E4 command programmatically, but these approaches don't
> seem very elegant.
>
> Thanks,
> Uwe
Re: E4 DI in &quot;onAction&quot; methods [message #1384682 is a reply to message #1384680] Fri, 30 May 2014 10:37 Go to previous message
Uwe San is currently offline Uwe SanFriend
Messages: 119
Registered: January 2012
Senior Member
This would be a working approach, but it comes with one drawback: the code does not clearly state that the Foo instance is needed specifically for the handleSelection() method. The controller needs to have a foo field when foo could simply be a method parameter.
Previous Topic:[Ann] Nightly builds have DnD support
Next Topic:Resize a Part in pixel
Goto Forum:
  


Current Time: Fri Apr 26 21:37:32 GMT 2024

Powered by FUDForum. Page generated in 0.03048 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top