[API Review] RT-27887: introduce a node to embed Swing into JavaFX
Scott Palmer
swpalmer at gmail.com
Fri Jan 25 11:38:38 PST 2013
The ugly code is what we need to do already when working in the other
direction with JFXPanel.
Some helpers that call out to factory methods on the right thread might be
nice for both cases.
Scott
On Fri, Jan 25, 2013 at 2:30 PM, Artem Ananiev <artem.ananiev at oracle.com>wrote:
>
> On 1/25/2013 8:30 PM, Richard Bair wrote:
>
>> I wonder if it's not better to require that the method is called
>>>> *always* in the swing thread to avoid confusion, I fear that some
>>>> programmers will construct their swing components outside the EDT, but
>>>> even worse, I fear this can be taken as some kind of allowance to
>>>> initialise them, and perhaps in some cases even start them, in the FX
>>>> dispatching thread.
>>>>
>>>> For no reason this consideration should pollute the API, but I guess
>>>> either requiring that the method is called in the EDT or putting in the
>>>> JavaDoc that the Swing components still need to follow the usual rules
>>>> would be a better thing.
>>>>
>>>
>>> Well, I think we may disallow calling it on FX thread, thus
>>> eliminating doubtful options.
>>>
>>
>> I'm actually of the other opinion, that it ought to be callable from
>> the FX thread but not the EDT. The reason is that there is no API
>> presently in FX that cannot be called on the FX thread (some APIs on
>> Service are spec'd to be called by the platform on a different
>> thread, but if you did call it from the FX thread it should still
>> work).
>>
>
> Just imagine the code that creates a SwingNode and its content:
>
> public void start(Stage stage) {
> Scene scene = ...;
> final SwingNode swing = new SwingNode();
> scene.getRoot().getChildren().**add(swing);
> stage.setScene(scene);
> stage.show();
>
> SwingUtilities.invokeLater(() -> {
> JComponent content = ...;
> swing.setContent(content);
> });
> }
>
> If we require setContent() to be called on FX thread only, the last part
> of the code above will become a little bit more ugly:
>
> SwingUtilities.invokeLater(() -> {
> final JComponent content = ...;
> Platform.runLater(() -> {
> swing.setContent(content);
> });
> });
>
>
> Another thing to think about (and I hope there is a JIRA for it) is
>> that I believe Artem had some ideas around how we could make the FX
>> thread and the EDT the same thread -- maybe by launching the JRE with
>> a command line flag. In that case the distinction would be
>> irrelevant.
>>
>
> Even if we implement single-threaded mode for FX/Swing bridge, it will be
> off by default for at least one major release to preserve backwards
> compatibility.
>
> Thanks,
>
> Artem
>
> Richard
>>
>
More information about the openjfx-dev
mailing list