[API Review] RT-27887: introduce a node to embed Swing into JavaFX
Mario Torre
neugens at redhat.com
Fri Jan 25 02:28:58 PST 2013
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.
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.
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.
How things like sizing and visibility of the component are handled here?
Are those events being passed (and notified) to the underlying swing
widget?
Also, the API suggest no heavyweight component, does it means menues and
tooltip can't be used?
Cheers,
Mario
More information about the openjfx-dev
mailing list