Skip to content

Instantly share code, notes, and snippets.

@skrb
Created June 24, 2014 05:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/48a6137dd9159cb67201 to your computer and use it in GitHub Desktop.
Save skrb/48a6137dd9159cb67201 to your computer and use it in GitHub Desktop.
Table Sample
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
public class Album {
private ObjectProperty<String> title = new SimpleObjectProperty<>();
private ObjectProperty<String> artist = new SimpleObjectProperty<>();
public Album(String title, String artist) {
this.title.set(title);
this.artist.set(artist);
}
public String getTitle() {
return title.get();
}
public void setTitle(String title) {
this.title.set(title);
}
public String getArtist() {
return artist.get();
}
public void setArtist(String artist) {
this.artist.set(artist);
}
public ObjectProperty<String> titleProperty() {
return title;
}
public ObjectProperty<String> artistProperty() {
return artist;
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AlbumTable extends Application {
ObservableList<Album> albums
= FXCollections.observableArrayList(
new Album("Buffalo Springfield", "Buffalo Springfield"),
new Album("Last Time Around", "Buffalo Springfield"),
new Album("After the Gold Rush", "Neil Young"),
new Album("Deja Vu", "Crosby, Stills, Nash & Young"),
new Album("Harvest", "Neil Young"),
new Album("Tonight's the Night", "Neil Young"),
new Album("Long May You Run", "Neil Young"),
new Album("CSN", "Crosby, Stills & Nash"),
new Album("Rust Never Sleeps", "Neil Young"),
new Album("Hawks & Doves", "Neil Young"),
new Album("American Dream", "Crosby, Stills, Nash & Young"),
new Album("Harvest Moon", "Neil Young")
);
@Override
public void start(Stage stage) {
VBox root = new VBox(12);
Scene scene = new Scene(root, 400, 300);
initHBox(root);
TableView<Album> table = new TableView<>();
table.setItems(albums);
// 列を構成する
TableColumn<Album, String> titleColumn = new TableColumn<>("Title");
titleColumn.setMinWidth(200);
titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
TableColumn<Album, String> artistColumn = new TableColumn<>("Artist");
artistColumn.setMinWidth(200);
artistColumn.setCellValueFactory(new PropertyValueFactory<>("artist"));
// テーブルに列を設定する
table.getColumns().addAll(titleColumn, artistColumn);
root.getChildren().add(table);
VBox.setVgrow(table, Priority.SOMETIMES);
stage.setTitle("Album Table");
stage.setScene(scene);
stage.show();
}
private void initHBox(VBox root) {
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(10));
root.getChildren().add(hbox);
TextField indexField = new TextField();
indexField.setPromptText("index");
TextField artistFiled = new TextField();
artistFiled.setPromptText("artist");
Button update = new Button("Update");
hbox.getChildren().addAll(indexField, artistFiled, update);
// プロパティの値を変更
update.setOnAction(e -> {
int index = Integer.parseInt(indexField.getText());
String artist = artistFiled.getText();
albums.get(index).setArtist(artist);
});
}
public static void main(String... args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment