[API Review] RT-27887: introduce a node to embed Swing into JavaFX
Anton V. Tarasov
anton.tarasov at oracle.com
Fri Jan 25 05:02:17 PST 2013
Hi Mario,
On 25.01.2013 14:28, Mario Torre wrote:
> Il giorno gio, 24/01/2013 alle 22.49 +0400, Anton V. Tarasov ha scritto:
>> Hi All,
>>
>> Please, review a request to add a new SwingNode class:
>>
>> http://javafx-jira.kenai.com/browse/RT-27887
>>
>> /**
>> * This class is used to embed a Swing content into a JavaFX application.
>> * The content to be displayed is specified with the {@link
>> #setContent} method
>> * that accepts an instance of Swing {@code JComponent}. The hierarchy
>> of components
>> * contained in the {@code JComponent} instance should not contain any
>> heavyweight
>> * components, otherwise {@code SwingNode} may fail to paint it. The
>> content gets
>> * repainted automatically. All the input and focus events are
>> forwarded to the
>> * {@code JComponent} instance transparently to the developer.
>> *<p>
>> * Here is a typical pattern which demonstrates how {@code SwingNode}
>> can be used:
>> *<pre>
>> * public class SwingFx extends Application {
>> *
>> * private SwingNode swingNode;
>> *
>> *@Override
>> * public void start(Stage stage) {
>> * swingNode = new SwingNode();
>> *
>> * createAndSetSwingContent();
>> *
>> * StackPane pane = new StackPane();
>> * pane.getChildren().add(swingNode);
>> *
>> * stage.setScene(new Scene(pane, 100, 50));
>> * stage.show();
>> * }
>> *
>> * private void createAndSetSwingContent() {
>> * SwingUtilities.invokeLater(new Runnable() {
>> *@Override
>> * public void run() {
>> * swingNode.setContent(new JButton("Click me!"));
>> * }
>> * });
>> * }
>> * }
>> *</pre>
>> */
> The API is neat, ideally I would not add any method for disposing, since
> this should be handled by the platform probably, but is an interesting
> topic, especially regarding animated content.
Similar problems exist for WebView, where it should freeze its rendering on becoming invisible.
>
> I have some doubt regarding setContent:
>
> * Attaches a JComponent instance to display in this SwingNode.
> * The method can be called either on the event dispatch thread or the
> * JavaFX application thread.
>
> The only way this method can be called by both threads and work is that
> you call yourself SwingUtilities.invokeLater inside it.
Right. This indeed is kind of a useless option, unless we make this call synchronous (via the nested
event loop mechanism).
And even this behavior is what a user can implement on his/her side.
>
> 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.
>
> How things like sizing and visibility of the component are handled here?
Visibility and size settings are forwarded to the Swing content. Regarding size values used in
layout (min, pref, max),
it's not yet completely solved. Currently, these methods return default values. I'm to investigate
if we can fix it.
>
> Are those events being passed (and notified) to the underlying swing
> widget?
Hope I answered this question above.
>
> Also, the API suggest no heavyweight component, does it means menues and
> tooltip can't be used?
Heavyweight popup windows, not being a part of the hierarchy, are allowed (as Anthony mentioned).
Thanks,
Anton.
>
> Cheers,
> Mario
>
>
More information about the openjfx-dev
mailing list