GeezerVison is showing github.com — JavaFX properties. · 4CGO/Source-Code-from-Tutorials@5469feb
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import javafx.application.Application; |
| 2 | +import javafx.scene.Scene; |
| 3 | +import javafx.scene.control.Button; |
| 4 | +import javafx.scene.layout.StackPane; |
| 5 | +import javafx.stage.Stage; |
| 6 | + |
| 7 | +public class Main extends Application { |
| 8 | + |
| 9 | + Stage window; |
| 10 | + Button button; |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + launch(args); |
| 14 | + } |
| 15 | + |
| 16 | + @Override |
| 17 | + public void start(Stage primaryStage) throws Exception { |
| 18 | + window = primaryStage; |
| 19 | + window.setTitle("thenewboston"); |
| 20 | + |
| 21 | + Person bucky = new Person(); |
| 22 | + |
| 23 | + bucky.firstNameProperty().addListener( (v, oldValue, newValue) -> { |
| 24 | + System.out.println("Name changed to " + newValue); |
| 25 | + System.out.println("firstNameProperty(): " + bucky.firstNameProperty()); |
| 26 | + System.out.println("getFirstName(): " + bucky.getFirstName()); |
| 27 | + }); |
| 28 | + |
| 29 | + button = new Button("Submit"); |
| 30 | + button.setOnAction(e -> bucky.setFirstName("Porky")); |
| 31 | + |
| 32 | + StackPane layout = new StackPane(); |
| 33 | + layout.getChildren().add(button); |
| 34 | + Scene scene = new Scene(layout, 300, 250); |
| 35 | + window.setScene(scene); |
| 36 | + window.show(); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +} |
Collapse file
Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import javafx.beans.property.SimpleStringProperty; |
| 2 | +import javafx.beans.property.StringProperty; |
| 3 | + |
| 4 | +public class Person { |
| 5 | + |
| 6 | + private StringProperty firstName = new SimpleStringProperty(this, "firstName", ""); |
| 7 | + |
| 8 | + //Returns the StringProperty object |
| 9 | + public StringProperty firstNameProperty() { |
| 10 | + return firstName; |
| 11 | + } |
| 12 | + |
| 13 | + //Return the firstName value (ie. "Bucky") |
| 14 | + public String getFirstName() { |
| 15 | + return firstName.get(); |
| 16 | + } |
| 17 | + |
| 18 | + //Set the firstName value |
| 19 | + public void setFirstName(String firstName) { |
| 20 | + this.firstName.set(firstName); |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | +} |