After JavaFX Script was abandoned, it took a long time to care about JavaFX. It happened that JavaFX has grown to 2.2 and has been bound to JDK 7. It seems that Oracle still attaches great importance to it. I saw a question in the Forum about whether TWaver has the JavaFX version. So I went to the Oracle website and found that JavaFX can be integrated into the Swing program. It was a good news. I immediately wrote a Demo, it is easy to integrate.
Running Effect
Code Analysis
A typical Network is in the middle of a form, and each of the top and bottom is a JavaFX container. The above container contains a Slider. When sliding, you can change the RenderColor of the Node selected by TWaver, the following containers contain some Text. When you select a Node in the Network and drag it, the name and location of the Node are displayed on the Text component. In this Demo, the program architecture is still Swing. JavaFX provides a JFXPanel component that can be embedded into the Swing container. JavaFX components can be placed in the JFXPanel. Note that, the Swing and JavaFX threads are different. the official Original article is: "JavaFX data shocould be accessed only on the JavaFX User thread", "Swing data shocould be changed only on the EDT ", therefore, if you want to modify JavaFX data in Swing, use Platform # runLater. In this example, modify the code of the Text Component of JavaFX when dragging Node:
1 network. getDataBox (). addElementPropertyChangeListener (new PropertyChangeListener (){
2 @ Override
3 public void propertyChange (PropertyChangeEvent evt ){
4 if ("location". equals (evt. getPropertyName ())){
5 Element element = (Element) evt. getSource ();
6 final String name = element. getName ();
7 final double x = element. getX ();
8 final double y = element. getY ();
9 Platform. runLater (new Runnable (){
10 @ Override
11 public void run (){
12 nameText. setText (name );
13 locationText. setText ("X:" + x + "Y:" + y );
14}
15 });
16}
17}
18 });
Similarly, if you want to modify Swing data in JavaFX, you also need to use SwingUtilities # invokeLater to wrap it. In this example, the following code slides the Slider to modify the Render Color of the selected Node:
1 slider. valueProperty (). addListener (new ChangeListener <Number> (){
2 public void changed (ObservableValue <? Extends Number> ov,
3 Number old_val, final Number new_val ){
4 SwingUtilities. invokeLater (new Runnable (){
5 @ Override
6 public void run (){
7 Iterator it = network. getSelectionModel (). selection ();
8 while (it. hasNext ()){
9 Element element = (Element) it. next ();
10 element. putRenderColor (new java. awt. Color (new_val.intValue ()));
11}
12}
13 });
14}
15 });
Complete the Code:
1 public class Test1 {
2 static TNetwork network;
3 static Text nameText;
4 static Text locationText;
5 static Slider slider;
6 private static void initAndShowGUI (){
7 initNetwork ();
8 JFrame frame = new JFrame ("TWaver With JavaFX ");
9 frame. getContentPane (). setLayout (new BorderLayout ());
10
11 frame. setSize (500,300 );
12 frame. setVisible (true );
13 frame. getContentPane (). add (network );
14
15 final JFXPanel controlFXPanel = new JFXPanel ();
16 frame. add (controlFXPanel, BorderLayout. NORTH );
17
18 final JFXPanel statusFXPanel = new JFXPanel ();
19 frame. add (statusFXPanel, BorderLayout. SOUTH );
20
21 frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
22 network. getDataBox (). addElementPropertyChangeListener (new PropertyChangeListener (){
23 @ Override
24 public void propertyChange (PropertyChangeEvent evt ){
25 if ("location". equals (evt. getPropertyName ())){
26 Element element = (Element) evt. getSource ();
27 final String name = element. getName ();
28 final double x = element. getX ();
29 final double y = element. getY ();
30 Platform. runLater (new Runnable (){
31 @ Override
32 public void run (){
33 nameText. setText (name );
34 locationText. setText ("X:" + x + "Y:" + y );
35}
36 });
37}
38}
39 });
40 Platform. runLater (new Runnable (){
41 @ Override
42 public void run (){
43 initFX (statusFXPanel, controlFXPanel );
44}
45 });
46}
47 private static void initNetwork (){
48 network = new TNetwork ();
49 twaver. Node node1 = new Node ();
50 node1.setName ("node1 ");
51 twaver. Node node2 = new Node ();
52 node2.setLocation (200,100 );
53 node2.setName ("node2 ");
54 Link link = new Link (node1, node2 );
55 network. getDataBox (). addElement (node1 );
56 network. getDataBox (). addElement (node2 );
57 network. getDataBox (). addElement (link );
58
59}
60 private static void initFX (JFXPanel statusFXPanel, JFXPanel controlFXPanel ){
61 Scene scene = createStatusScene ();
62 statusFXPanel. setScene (scene );
63 statusFXPanel. validate ();
64
65 Scene controlScene = createControlScene ();
66 controlFXPanel. setScene (controlScene );
67 controlFXPanel. validate ();
68
69}
70 private static Scene createControlScene (){
71 Group root = new Group ();
72 Scene scene = new Scene (root, Color. ALICEBLUE );
73 HBox hbox = new HBox ();
74 slider = new Slider (0,16777215, 0 );
75 slider. setPrefHeight (30 );
76 slider. setPrefWidth (300 );
77 slider. valueProperty (). addListener (new ChangeListener <Number> (){
78 public void changed (ObservableValue <? Extends Number> ov,
79 Number old_val, final Number new_val ){
80 SwingUtilities. invokeLater (new Runnable (){
81 @ Override
82 public void run (){
83 Iterator it = network. getSelectionModel (). selection ();
84 while (it. hasNext ()){
85 Element element = (Element) it. next ();
86 element. putRenderColor (new java. awt. Color (new_val.intValue ()));
87}
88}
89 });
90}
91 });
92 Label label = new Label ("color :");
93 label. setPrefHeight (30 );
94 hbox. getChildren (). add (label );
95 hbox. getChildren (). add (slider );
96 root. getChildren (). add (hbox );
97 return scene;
98}
99 private static Scene createStatusScene (){
100 Group root = new Group ();
101 Scene scene = new Scene (root, Color. ALICEBLUE );
102 VBox vbox = new VBox ();
103 vbox. setPrefWidth (300 );
104 vbox. setPrefHeight (20 );
105 HBox hbox = new HBox ();
106 nameText = new Text ();
107 hbox. getChildren (). add (new Label ("Name :"));
108 hbox. getChildren (). add (nameText );
109 hbox. getChildren (). add (new Text (""));
110 locationText = new Text ();
111 hbox. getChildren (). add (new Label ("Location :"));
112 hbox. getChildren (). add (locationText );
113 vbox. getChildren (). add (hbox );
114 root. getChildren (). add (vbox );
115 return (scene );
116}
117
118 public static void main (String [] args ){
119 SwingUtilities. invokeLater (new Runnable (){
120 @ Override
121 public void run (){
122 initAndShowGUI ();
123}
124 });
125}
126}