|
| 1 | +package javafx.enlaces; |
| 2 | + |
| 3 | +import javafx.application.Application; |
| 4 | +import javafx.beans.binding.Bindings; |
| 5 | +import javafx.beans.binding.DoubleBinding; |
| 6 | +import javafx.beans.binding.NumberBinding; |
| 7 | +import javafx.geometry.Insets; |
| 8 | +import javafx.scene.Parent; |
| 9 | +import javafx.scene.Scene; |
| 10 | +import javafx.scene.control.Button; |
| 11 | +import javafx.scene.control.Label; |
| 12 | +import javafx.scene.control.ListView; |
| 13 | +import javafx.scene.control.PasswordField; |
| 14 | +import javafx.scene.control.ProgressBar; |
| 15 | +import javafx.scene.control.Slider; |
| 16 | +import javafx.scene.control.Spinner; |
| 17 | +import javafx.scene.control.SpinnerValueFactory.DoubleSpinnerValueFactory; |
| 18 | +import javafx.scene.control.TextField; |
| 19 | +import javafx.scene.layout.ColumnConstraints; |
| 20 | +import javafx.scene.layout.GridPane; |
| 21 | +import javafx.scene.layout.Priority; |
| 22 | +import javafx.scene.layout.VBox; |
| 23 | +import javafx.stage.Stage; |
| 24 | + |
| 25 | +public class JavaFXEnlaces extends Application { |
| 26 | + |
| 27 | + @Override |
| 28 | + public void start(Stage primaryStage) { |
| 29 | + |
| 30 | + Scene scene = new Scene(createAreaBind()); |
| 31 | + |
| 32 | + primaryStage.setTitle("JavaFX Boolean Binding"); |
| 33 | + primaryStage.setScene(scene); |
| 34 | + primaryStage.show(); |
| 35 | + } |
| 36 | + |
| 37 | + private Parent createConvertBind() { |
| 38 | + |
| 39 | + Label lbl = new Label("Conversor de longitud, en Pies:"); |
| 40 | + lbl.setStyle("-fx-font-size: 18;"); |
| 41 | + |
| 42 | + Slider sliderPies = new Slider(0, 100, 0); |
| 43 | + sliderPies.setShowTickMarks(true); |
| 44 | + sliderPies.setShowTickLabels(true); |
| 45 | + |
| 46 | + NumberBinding pulgadas = sliderPies.valueProperty().multiply(12); |
| 47 | + NumberBinding metros = sliderPies.valueProperty().multiply(.3048); |
| 48 | + |
| 49 | + TextField txtPulgadas = new TextField(); |
| 50 | + txtPulgadas.textProperty().bind(pulgadas.asString("En pulgadas: %.2f")); |
| 51 | + |
| 52 | + TextField txtMetros = new TextField(); |
| 53 | + txtMetros.textProperty().bind(metros.asString("En metros: %.2f")); |
| 54 | + |
| 55 | + VBox root = new VBox(lbl, sliderPies, txtMetros, txtPulgadas); |
| 56 | + root.setPadding(new Insets(10.0)); |
| 57 | + root.setSpacing(10.0); |
| 58 | + |
| 59 | + return root; |
| 60 | + } |
| 61 | + |
| 62 | + private Parent createBooleanBind() { |
| 63 | + |
| 64 | + Label lblNombre = new Label("Nombre"); |
| 65 | + Label lblContrasena = new Label("Contrasena"); |
| 66 | + |
| 67 | + TextField tfNombre = new TextField(); |
| 68 | + PasswordField tfContrasena = new PasswordField(); |
| 69 | + |
| 70 | + Button btnEntrar = new Button("Entrar"); |
| 71 | + btnEntrar.setMaxWidth(Double.MAX_VALUE); |
| 72 | + |
| 73 | + btnEntrar.disableProperty().bind( |
| 74 | + tfNombre.textProperty().isEmpty().or( |
| 75 | + tfContrasena.textProperty().isEmpty())); |
| 76 | + |
| 77 | + GridPane.setColumnSpan(btnEntrar, 2); |
| 78 | + |
| 79 | + GridPane root = new GridPane(); |
| 80 | + root.setPadding(new Insets(10.0)); |
| 81 | + root.add(lblNombre, 0, 0); |
| 82 | + root.add(lblContrasena, 0, 1); |
| 83 | + root.add(tfNombre, 1, 0); |
| 84 | + root.add(tfContrasena, 1, 1); |
| 85 | + root.add(btnEntrar, 0, 2); |
| 86 | + root.setHgap(10.0); |
| 87 | + root.setVgap(5.0); |
| 88 | + |
| 89 | + ColumnConstraints column1 = new ColumnConstraints(); |
| 90 | + |
| 91 | + ColumnConstraints column2 = new ColumnConstraints(350, 100, Double.MAX_VALUE); |
| 92 | + column2.setHgrow(Priority.ALWAYS); |
| 93 | + |
| 94 | + root.getColumnConstraints().addAll(column1, column2); |
| 95 | + |
| 96 | + return root; |
| 97 | + } |
| 98 | + |
| 99 | + private Parent createAreaBind() { |
| 100 | + |
| 101 | + Spinner<Double> sbBase = new Spinner(new DoubleSpinnerValueFactory(0, 100, 50.5)); |
| 102 | + Spinner<Double> sbAltura = new Spinner(new DoubleSpinnerValueFactory(0, 100, 20.5)); |
| 103 | + |
| 104 | + DoubleBinding base = Bindings.selectDouble(sbBase, "valueFactory", "value"); |
| 105 | + DoubleBinding altura = Bindings.selectDouble(sbAltura, "valueFactory", "value"); |
| 106 | + |
| 107 | + NumberBinding area = Bindings.multiply(base, altura); |
| 108 | + |
| 109 | + TextField txt = new TextField(); |
| 110 | + txt.textProperty().bind(area.asString("Area del rectangulo: %.2f")); |
| 111 | + |
| 112 | + TextField txt0 = new TextField(); |
| 113 | + txt0.textProperty().bind(area.divide(2.0).asString("Area del triangulo: %.2f")); |
| 114 | + |
| 115 | + VBox root = new VBox(sbBase, sbAltura, txt, txt0); |
| 116 | + root.setPadding(new Insets(10.0)); |
| 117 | + root.setSpacing(10.0); |
| 118 | + |
| 119 | + return root; |
| 120 | + } |
| 121 | + |
| 122 | + private Parent createCounterBind() { |
| 123 | + |
| 124 | + ListView<Integer> lv = new ListView<>(); |
| 125 | + lv.getItems().addAll(1, 2, 3, 4, 5); |
| 126 | + |
| 127 | + String txt = "Elemento seleccionado: %d"; |
| 128 | + |
| 129 | + Label lbl = new Label(); |
| 130 | + lbl.textProperty().bind(lv.getSelectionModel() |
| 131 | + .selectedItemProperty().asString(txt)); |
| 132 | + |
| 133 | + VBox root = new VBox(lbl, lv); |
| 134 | + root.setPadding(new Insets(10.0)); |
| 135 | + root.setSpacing(10.0); |
| 136 | + |
| 137 | + return root; |
| 138 | + } |
| 139 | + |
| 140 | + private Parent createSimpleBidirectionalBind() { |
| 141 | + |
| 142 | + TextField tf_1 = new TextField(); |
| 143 | + TextField tf_2 = new TextField(); |
| 144 | + |
| 145 | + tf_1.textProperty().bindBidirectional(tf_2.textProperty()); |
| 146 | + |
| 147 | + VBox root = new VBox(tf_1, tf_2); |
| 148 | + root.setPadding(new Insets(10.0)); |
| 149 | + root.setSpacing(10.0); |
| 150 | + |
| 151 | + return root; |
| 152 | + } |
| 153 | + |
| 154 | + private VBox createSimpleBind() { |
| 155 | + Slider slider = new Slider(0, 1, 0); |
| 156 | + |
| 157 | + ProgressBar bar = new ProgressBar(0); |
| 158 | + bar.setMaxWidth(Double.MAX_VALUE); |
| 159 | + bar.progressProperty().bind(slider.valueProperty()); |
| 160 | + |
| 161 | + bar.progressProperty().unbind(); |
| 162 | + bar.progressProperty().set(0.5); |
| 163 | + |
| 164 | + VBox root = new VBox(slider, bar); |
| 165 | + root.setPadding(new Insets(10.0)); |
| 166 | + root.setSpacing(10.0); |
| 167 | + |
| 168 | + return root; |
| 169 | + } |
| 170 | + |
| 171 | + public static void main(String[] args) { |
| 172 | + launch(args); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments