I have been paying attention to the development of javafx since July December.
Now, it's almost the fifth year. During this period, I experienced some turbulence, but javafx was still developing and had undergone many changes (this is also a feature of Java technology, it won't be like Microsoft's abandonment of technology and frequent upgrades). From the pure javafx scripting language that used to adapt to the development of rich Internet technology, it has now become a technology completely used to replace swing.
Although it is to replace swing, any technology must be upgraded gradually to avoid a great impact. If you simply remove swing from JDK and add javafx, it will make countless programs in the world unable to run, and the impact will be immeasurable (just like there are many unreasonable old class libraries in JDK, even if there is a new alternative solution, we cannot completely delete the previous one, so JDK is actually getting bloated ). Here, we will introduce how to use javafx in swing.
First, we need to understand that, whether it is swing or javafx, Gui construction must be carried out in the respective EDT (event distribution thread), which is also the cause of error when operating the UI in other threads.
In javafx, jfxpanel is added. This component serves as a bridge between javafx and swing for mixed programming.
Let's take a look at the inheritance relationship of jfxpanel.
As you can see, jfxpanel inherits from jcomponent. The UI controls of javafx are inherited from javafx. Scene. Control. Control. Obviously, it is actually a swing component, not a javafx component.
Let's take a look at the inheritance relationship of jpanel.
The inheritance relationships are basically the same. That is to say, any place where jpanel is used can be replaced by jfxpanel. Jfxpanel has a setscene method. You can set the javafx content displayed in the method.
According to the document introduction, the setscene method can be used in edts of swing and javafx. It can also be said that setscene is the key to combining javafx and swing.
However, the creation of javafx GUI also needs to be performed in the event distribution thread of javafx. Therefore, we need another platform class.
In the middle of platform. runlater (New runnable (), the event distribution thread of javafx is executed. We can create javafx UI in runnable.
The following is a simple example-embedding a javafx Web browser in swing.
import java.awt.BorderLayout;import java.awt.Toolkit;import javafx.application.Platform;import javafx.embed.swing.JFXPanel;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.TextField;import javafx.scene.layout.HBox;import javafx.scene.layout.VBox;import javafx.scene.web.WebEngine;import javafx.scene.web.WebView;import javax.swing.JFrame;public class MainClass {private static final int WIDTH = 800;private static final int HEIGHT = 600;private static final String url = "http://blog.csdn.net/ml3947";private static final String urlStart = "http://";/** * @param args */public static void main(String[] args) {JFrame frame = new JFrame("JavaFX in Swing");final JFXPanel webBrowser = new JFXPanel();frame.setLayout(new BorderLayout());frame.add(webBrowser, BorderLayout.CENTER);Platform.runLater(new Runnable() {@Overridepublic void run() {Group root = new Group();Scene scene = new Scene(root, WIDTH, HEIGHT);webBrowser.setScene(scene);Double widthDouble = new Integer(WIDTH).doubleValue();Double heightDouble = new Integer(HEIGHT).doubleValue();VBox box = new VBox(10);HBox urlBox = new HBox(10);final TextField urlTextField = new TextField();urlTextField.setText(url);Button go = new Button("go");urlTextField.setPrefWidth(WIDTH - 70);urlBox.getChildren().addAll(urlTextField, go);WebView view = new WebView();view.setMinSize(widthDouble, heightDouble);view.setPrefSize(widthDouble, heightDouble);final WebEngine eng = view.getEngine();eng.load(url);root.getChildren().add(view);box.getChildren().add(urlBox);box.getChildren().add(view);root.getChildren().add(box);go.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {if (!urlTextField.getText().startsWith(urlStart)) {eng.load(urlStart + urlTextField.getText());} else {eng.load(urlTextField.getText());}}});}});int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(WIDTH, HEIGHT);frame.setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2);frame.setVisible(true);}}
This is a simple example.
We have created jframe and jfxpanel, created scene in platform. runlater, and set it to jfxpanel.
We created a textfield to enter the URL and a javafx button to jump to the URL. Determine whether the URL starts with http: // and perform simple processing.
Let's take a look at the running effect:
.
In addition, jfxcanvas is used in SWT. Due to implementation mechanism problems, we do not need to use the platform class in swing. Jfxcanvas is also a direct subclass of org. Eclipse. SWT. Widgets. Canvas, which can be used directly.
Example:
public class JFXInSwt { private static Scene createScene() { Group group = new Group(); Scene scene = new Scene(group); Button button = new Button("JFX Button"); group.getChildren().add(button); return scene; } public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); FXCanvas canvas = new FXCanvas(shell, SWT.NONE); Scene scene = createScene(); canvas.setScene(scene); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Bytes ---------------------------------------------------------------------------------------
Unfortunately, the project homepage of SourceForge seems to have been blocked, even if I have bound a top-level domain name of my own... it is also sometimes accessible and sometimes inaccessible, so the example is not prepared to be placed above. After a while, buy a space and build your own site.
Bytes ---------------------------------------------------------------------------------------