Created
December 15, 2017 20:32
-
-
Save bsdis/c9f39e334dadb10438a88ccb93925326 to your computer and use it in GitHub Desktop.
java fxml standalone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
import java.time.LocalDate; | |
import javafx.beans.property.IntegerProperty; | |
import javafx.beans.property.ObjectProperty; | |
import javafx.beans.property.SimpleIntegerProperty; | |
import javafx.beans.property.SimpleObjectProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
public class Foobar extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
ObservableList<Person> personData = FXCollections.observableArrayList(); | |
personData.add(new Person("Hans", "Muster")); | |
primaryStage.setTitle("Hello World!"); | |
Button btn = new Button(); | |
TableView<Person> personTable = new TableView<Person>(); | |
TableColumn<Person, String> firstNameColumn = new TableColumn<Person, String>(); | |
TableColumn<Person, String> lastNameColumn = new TableColumn<Person, String>(); | |
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty()); | |
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty()); | |
personTable.setItems(personData); | |
btn.setText("Say 'Hello World'"); | |
btn.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
System.out.println("clicked"); | |
} | |
}); | |
StackPane root = new StackPane(); | |
root.getChildren().add(btn); | |
root.getChildren().add(personTable); | |
primaryStage.setScene(new Scene(root, 300, 250)); | |
primaryStage.show(); | |
} | |
} | |
class Person { | |
private final StringProperty firstName; | |
private final StringProperty lastName; | |
private final StringProperty street; | |
private final IntegerProperty postalCode; | |
private final StringProperty city; | |
private final ObjectProperty<LocalDate> birthday; | |
/** | |
* Default constructor. | |
*/ | |
public Person() { | |
this(null, null); | |
} | |
/** | |
* Constructor with some initial data. | |
* | |
* @param firstName | |
* @param lastName | |
*/ | |
public Person(String firstName, String lastName) { | |
this.firstName = new SimpleStringProperty(firstName); | |
this.lastName = new SimpleStringProperty(lastName); | |
// Some initial dummy data, just for convenient testing. | |
this.street = new SimpleStringProperty("some street"); | |
this.postalCode = new SimpleIntegerProperty(1234); | |
this.city = new SimpleStringProperty("some city"); | |
this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21)); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String firstName) { | |
this.firstName.set(firstName); | |
} | |
public StringProperty firstNameProperty() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String lastName) { | |
this.lastName.set(lastName); | |
} | |
public StringProperty lastNameProperty() { | |
return lastName; | |
} | |
public String getStreet() { | |
return street.get(); | |
} | |
public void setStreet(String street) { | |
this.street.set(street); | |
} | |
public StringProperty streetProperty() { | |
return street; | |
} | |
public int getPostalCode() { | |
return postalCode.get(); | |
} | |
public void setPostalCode(int postalCode) { | |
this.postalCode.set(postalCode); | |
} | |
public IntegerProperty postalCodeProperty() { | |
return postalCode; | |
} | |
public String getCity() { | |
return city.get(); | |
} | |
public void setCity(String city) { | |
this.city.set(city); | |
} | |
public StringProperty cityProperty() { | |
return city; | |
} | |
public LocalDate getBirthday() { | |
return birthday.get(); | |
} | |
public void setBirthday(LocalDate birthday) { | |
this.birthday.set(birthday); | |
} | |
public ObjectProperty<LocalDate> birthdayProperty() { | |
return birthday; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment