One of the first things you try to do when experimenting
with JavaFX is to add a JavaFX scene graph to a Swing
component. As of JavaFX 1.1, there is no standard API for
doing that. This is a class we used at my company to
accomplish the task while evaluating JavaFX before deciding
against using JavaFX.
/*
* Copyright 2008 Savarese Software Research Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.savarese.com/software/ApacheLicense-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.savarese.jfx.ext.swing;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javafx.reflect.FXClassType;
import javafx.reflect.FXContext;
import javafx.reflect.FXLocal;
import javafx.reflect.FXObjectValue;
import javafx.reflect.FXValue;
public class JScene extends JComponent {
public JScene(Object obj) throws Exception {
setLayout(new BorderLayout());
FXClassType sceneImpl =
FXContext.getInstance().findClass("com.sun.javafx.scene.JSGPanelSceneImpl");
FXObjectValue scene = sceneImpl.allocate();
scene.initVar("scene", FXLocal.getContext().mirrorOf(obj));
scene.initialize();
FXValue panel = sceneImpl.getVariable("jsgPanel").getValue(scene);
add((JComponent)((FXLocal.ObjectValue)panel).asObject());
}
}
Use the class as follows in your JavaFX code:
import javafx.ext.swing.SwingComponent;
import com.savarese.jfx.ext.swing.JScene;
import com.savarese.jfx.ext.swing.SplitPane;
...
scene: Scene {
content: [
...
SplitPane {
left: ...,
right: SwingComponent.wrap(new JScene(foo_scene))
}
]
...
}
...
You'll notice the use of a custom SplitPane class. We
had to implement custom MenuBar, Menu, MenuItem, etc., making
JavaFX a waste of time for desktop apps. For interested
parties, here's SplitPane:
/*
* Copyright 2008 Savarese Software Research Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.savarese.com/software/ApacheLicense-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.savarese.jfx.ext.swing;
import javafx.ext.swing.SwingComponent;
import javax.swing.JSplitPane;
public def HorizontalSplit : Integer = JSplitPane.HORIZONTAL_SPLIT;
public def VerticalSplit : Integer = JSplitPane.VERTICAL_SPLIT;
public class SplitPane extends SwingComponent {
var split_pane: JSplitPane;
public var orientation: Integer = HorizontalSplit on replace {
split_pane.setOrientation(orientation);
}
public var left: SwingComponent on replace {
split_pane.setLeftComponent(left.getJComponent());
}
public var right: SwingComponent on replace {
split_pane.setRightComponent(right.getJComponent());
}
protected override function createJComponent() {
split_pane = new JSplitPane();
return split_pane;
}
}
|