|
| 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 | +} |
0 commit comments