Skip to content

Commit 1708413

Browse files
committed
1st
0 parents  commit 1708413

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import java.lang.*?>
4+
<?import java.util.*?>
5+
<?import javafx.scene.control.*?>
6+
<?import javafx.scene.layout.*?>
7+
<?import javafx.scene.paint.*?>
8+
9+
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="javaFX2toggleButton.ToggleButtonController">
10+
<children>
11+
<VBox layoutX="20.0" layoutY="20.0" prefHeight="-1.0" prefWidth="-1.0" spacing="50.0">
12+
<children>
13+
<VBox prefHeight="-1.0" prefWidth="-1.0" spacing="5.0">
14+
<children>
15+
<Label text="* Using Event Handler" />
16+
<HBox prefHeight="-1.0" prefWidth="-1.0" spacing="10.0">
17+
<children>
18+
<ToggleButton fx:id="tgb" mnemonicParsing="false" onAction="#tgbOnAction" prefWidth="130.0" text="Toggle Button" />
19+
<TextField fx:id="txf" alignment="CENTER" editable="false" focusTraversable="false" prefWidth="100.0" style="-fx-background-color: beige" />
20+
</children>
21+
</HBox>
22+
</children>
23+
</VBox>
24+
<VBox prefHeight="-1.0" prefWidth="-1.0" spacing="5.0">
25+
<children>
26+
<Label text="* Using Bind (High-level API)" />
27+
<HBox prefHeight="-1.0" prefWidth="-1.0" spacing="10.0">
28+
<children>
29+
<ToggleButton fx:id="tgbHBind" mnemonicParsing="false" prefWidth="130.0" text="Toggle Button" />
30+
<TextField fx:id="txfHBind" alignment="CENTER" editable="false" focusTraversable="false" prefWidth="100.0" style="-fx-background-color: beige" />
31+
</children>
32+
</HBox>
33+
</children>
34+
</VBox>
35+
<VBox prefHeight="-1.0" prefWidth="-1.0" spacing="5.0">
36+
<children>
37+
<Label text="* Using Bind (Low-level API)" />
38+
<HBox prefHeight="-1.0" prefWidth="-1.0" spacing="10.0">
39+
<children>
40+
<ToggleButton fx:id="tgbLBind" mnemonicParsing="false" prefWidth="130.0" text="Toggle Button" />
41+
<TextField fx:id="txfLBind" alignment="CENTER" editable="false" focusTraversable="false" prefWidth="100.0" style="-fx-background-color: beige" />
42+
</children>
43+
</HBox>
44+
</children>
45+
</VBox>
46+
</children>
47+
</VBox>
48+
</children>
49+
</AnchorPane>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package javaFX2toggleButton;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.stage.Stage;
8+
9+
public class ToggleButton extends Application {
10+
11+
@Override
12+
public void start(Stage stage) throws Exception {
13+
14+
Parent root = FXMLLoader.load(this.getClass().getResource("ToggleButton.fxml"));
15+
Scene scene = new Scene(root);
16+
17+
stage.setTitle("JavaFX 2 toggle button");
18+
19+
stage.setScene(scene);
20+
stage.show();
21+
}
22+
23+
public static void main(String[] args) {
24+
launch(args);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package javaFX2toggleButton;
2+
3+
import javafx.beans.binding.ObjectBinding;
4+
import javafx.event.ActionEvent;
5+
import javafx.fxml.FXML;
6+
import javafx.scene.control.TextField;
7+
import javafx.scene.control.ToggleButton;
8+
import javafx.scene.image.Image;
9+
import javafx.scene.image.ImageView;
10+
11+
public class ToggleButtonController {
12+
13+
@FXML
14+
private ToggleButton tgb;
15+
@FXML
16+
private TextField txf;
17+
18+
@FXML
19+
private ToggleButton tgbHBind;
20+
@FXML
21+
private TextField txfHBind;
22+
23+
@FXML
24+
private ToggleButton tgbLBind;
25+
@FXML
26+
private TextField txfLBind;
27+
28+
private Image imgPowerOff;
29+
private Image imgPowerOn;
30+
31+
private String on = "ON";
32+
private String off = "OFF";
33+
34+
@FXML
35+
void initialize() {
36+
imgPowerOff = new Image(this.getClass().getResourceAsStream("res/powerOff.png"));
37+
imgPowerOn = new Image(this.getClass().getResourceAsStream("res/powerOn.png"));
38+
39+
// Using Event Handler
40+
assert tgb != null : "fx:id=\"tgb\" was not injected: check your FXML file 'ToggleButton.fxml'.";
41+
assert txf != null : "fx:id=\"txf\" was not injected: check your FXML file 'ToggleButton.fxml'.";
42+
this.tgb.setGraphic(new ImageView(imgPowerOff));
43+
this.tgb.setSelected(false);
44+
this.txf.setText(off);
45+
46+
// Using Bind (High-level API)
47+
assert tgbHBind != null : "fx:id=\"tgbHBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
48+
assert txfHBind != null : "fx:id=\"txfHBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
49+
this.tgbHBind.setGraphic(new ImageView(imgPowerOff));
50+
this.tgbHBind.setSelected(false);
51+
this.txfHBind.textProperty().bind(this.tgbHBind.selectedProperty().asString());
52+
53+
// Using Bind (Low-level API)
54+
assert tgbLBind != null : "fx:id=\"tgbLBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
55+
assert txfLBind != null : "fx:id=\"txfLBind\" was not injected: check your FXML file 'ToggleButton.fxml'.";
56+
this.tgbLBind.setGraphic(new ImageView(imgPowerOff));
57+
this.tgbLBind.setSelected(false);
58+
this.txfLBind.textProperty().bind(this.observer(tgbLBind));
59+
}
60+
61+
// Using Event Handler
62+
@FXML
63+
void tgbOnAction(ActionEvent event) {
64+
if (this.tgb.isSelected()) {
65+
this.txf.setText(on);
66+
this.tgb.setGraphic(new ImageView(imgPowerOn));
67+
}
68+
else {
69+
this.txf.setText(off);
70+
this.tgb.setGraphic(new ImageView(imgPowerOff));
71+
}
72+
}
73+
74+
// Using Bind (High-level API)
75+
76+
// Using Bind (Low-level API)
77+
private ObjectBinding<String> observer(ToggleButton p) {
78+
final ToggleButton tgb = p;
79+
ObjectBinding<String> sBinding = new ObjectBinding<String>() {
80+
{
81+
super.bind(tgb.selectedProperty());
82+
}
83+
84+
@Override
85+
protected String computeValue() {
86+
String s;
87+
if (tgb.isSelected()) {
88+
s = on;
89+
tgbLBind.setGraphic(new ImageView(imgPowerOn));
90+
91+
}
92+
else {
93+
s = off;
94+
tgbLBind.setGraphic(new ImageView(imgPowerOff));
95+
}
96+
return s;
97+
}
98+
};
99+
return sBinding;
100+
}
101+
}
1.17 KB
Loading
3.54 KB
Loading

0 commit comments

Comments
 (0)