-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggleButtonController.java
More file actions
executable file
·101 lines (85 loc) · 2.89 KB
/
Copy pathToggleButtonController.java
File metadata and controls
executable file
·101 lines (85 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package javaFX2toggleButton;
import javafx.beans.binding.ObjectBinding;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ToggleButtonController {
@FXML
private ToggleButton tgb;
@FXML
private TextField txf;
@FXML
private ToggleButton tgbHBind;
@FXML
private TextField txfHBind;
@FXML
private ToggleButton tgbLBind;
@FXML
private TextField txfLBind;
private Image imgPowerOff;
private Image imgPowerOn;
private String on = "ON";
private String off = "OFF";
@FXML
void initialize() {
imgPowerOff = new Image(this.getClass().getResourceAsStream("res/powerOff.png"));
imgPowerOn = new Image(this.getClass().getResourceAsStream("res/powerOn.png"));
// Using Event Handler
assert tgb != null : "fx:id=\"tgb\" was not injected: check your FXML file 'ToggleButton.fxml'.";
assert txf != null : "fx:id=\"txf\" was not injected: check your FXML file 'ToggleButton.fxml'.";
this.tgb.setGraphic(new ImageView(imgPowerOff));
this.tgb.setSelected(false);
this.txf.setText(off);
// Using Bind (High-level API)
assert tgbHBind != null : "fx:id=\"tgbHBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
assert txfHBind != null : "fx:id=\"txfHBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
this.tgbHBind.setGraphic(new ImageView(imgPowerOff));
this.tgbHBind.setSelected(false);
this.txfHBind.textProperty().bind(this.tgbHBind.selectedProperty().asString());
// Using Bind (Low-level API)
assert tgbLBind != null : "fx:id=\"tgbLBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
assert txfLBind != null : "fx:id=\"txfLBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
this.tgbLBind.setGraphic(new ImageView(imgPowerOff));
this.tgbLBind.setSelected(false);
this.txfLBind.textProperty().bind(this.observer(tgbLBind));
}
// Using Event Handler
@FXML
void tgbOnAction(ActionEvent event) {
if (this.tgb.isSelected()) {
this.txf.setText(on);
this.tgb.setGraphic(new ImageView(imgPowerOn));
}
else {
this.txf.setText(off);
this.tgb.setGraphic(new ImageView(imgPowerOff));
}
}
// Using Bind (High-level API)
// Using Bind (Low-level API)
private ObjectBinding<String> observer(ToggleButton p) {
final ToggleButton tgb = p;
ObjectBinding<String> sBinding = new ObjectBinding<String>() {
{
super.bind(tgb.selectedProperty());
}
@Override
protected String computeValue() {
String s;
if (tgb.isSelected()) {
s = on;
tgbLBind.setGraphic(new ImageView(imgPowerOn));
}
else {
s = off;
tgbLBind.setGraphic(new ImageView(imgPowerOff));
}
return s;
}
};
return sBinding;
}
}