Skip to content
Snippets Groups Projects
Commit a9823fb5 authored by dagk's avatar dagk
Browse files

Slides for onsdag uke 10, to JavaFX-eksempler på obervatør-pattern.

parent dba4a602
No related branches found
No related tags found
No related merge requests found
File added
package uke10.observerfx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Hello extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello");
Button btn = new Button();
btn.setText("Say 'Hello'");
btn.setOnAction(event -> System.out.println("Hello"));
// Med anonym klasse hadde dette blitt:
// btn.setOnAction(new EventHandler<ActionEvent> () {
// @Override
// public void handle(ActionEvent event) {
// System.out.println("Hello!");
// }
// });
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
\ No newline at end of file
package uke10.observerfx;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
TextField textField = new TextField();
Label label = new Label();
// Add a listener to the TextField's text property
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(observable); // for å logge kallet og hva observable-objektet inneholder
label.setText(newValue);
}
});
VBox vbox = new VBox(textField, label);
Scene scene = new Scene(vbox, 200, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment