Posted on 2012-09-26 10:38
TWaver 阅读(1203)
评论(0) 编辑 收藏
JavaFX Script被抛弃以后,很久不关心JavaFX了,偶然发现JavaFX已经发展到2.2,而且已经绑定到JDK7中,看来Oracle对它还是蛮重视的。
看到有人在论坛提问TWaver有没有JavaFX版本,于是去Oracle网站上逛了一下,发现可以将JavaFX集成进Swing程序中,真是福音啊,立马动手写了个Demo,集成起来还是很方便的。
运行效果
代码分析
窗体中间是个很典型的Network,上下各是一个JavaFX容器,上面容器中包含一个Slider,滑动时可以改变TWaver选中Node的RenderColor,下面的容器中包含一些Text,选中Network中的Node并拖动的时候在Text组件上显示Node的name和location。
在这个Demo里,程序架构还是Swing,JavaFX提供了一个名为JFXPanel的组件可以嵌入到Swing容器中,JFXPanel内可以放JavaFX的组件,需要注意,Swing和JavaFX的线程不同,官方的原文是:"JavaFX data should be accessed only on the JavaFX User thread","Swing data should be changed only on the EDT",所以如果要在Swing中修改JavaFX的数据,应该使用Platform#runLater包装,本例中,拖动Node时修改JavaFX的Text组件的相关代码:
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 });
同样,如果要在JavaFX中修改Swing数据,也要用SwingUtilities#invokeLater包装,本例中,滑动Slider修改选中Node的Render Color代码如下:
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 });
最后附上全部代码:
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.setDefaultCloseOperation(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("颜色:");
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 }