Skip to content

Commit 5469feb

Browse files
committed
JavaFX properties.
1 parent cd49f28 commit 5469feb

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

JavaFX/028_properties/Main.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

JavaFX/028_properties/Person.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)