Created
January 16, 2014 14:28
-
-
Save senleft/8455844 to your computer and use it in GitHub Desktop.
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
package tableviewsample; | |
import javafx.application.Application; | |
import javafx.beans.property.SimpleObjectProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableColumn.CellEditEvent; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.control.cell.TextFieldTableCell; | |
import javafx.scene.input.MouseButton; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
import javafx.util.converter.DateTimeStringConverter; | |
import java.util.Date; | |
public class FxTableViewSample extends Application { | |
private TableView<Person> table = new TableView<Person>(); | |
private Person person; | |
private final ObservableList<Person> data = | |
FXCollections.observableArrayList( | |
new Person("Jacob", "Smith", "jacob.smith@example.com", new Date()), | |
new Person("Isabella", "Johnson", "isabella.johnson@example.com", new Date()), | |
new Person("Ethan", "Williams", "ethan.williams@example.com", new Date()), | |
new Person("Emma", "Jones", "emma.jones@example.com", new Date()), | |
new Person("Michael", "Brown", "michael.brown@example.com", new Date())); | |
final HBox hb = new HBox(); | |
private TextField firstNameField; | |
private TextField lastNameField; | |
private TextField emailField; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) { | |
Scene scene = new Scene(new Group()); | |
stage.setTitle("Table View Sample"); | |
stage.setWidth(650); | |
stage.setHeight(550); | |
final Label label = new Label("Address Book"); | |
label.setFont(new Font("Arial", 20)); | |
table.setEditable(true); | |
TableColumn firstNameCol = new TableColumn("First Name"); | |
firstNameCol.setMinWidth(100); | |
firstNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("firstName")); | |
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
firstNameCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
(t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setFirstName(t.getNewValue()); | |
} | |
} | |
); | |
TableColumn lastNameCol = new TableColumn("Last Name"); | |
lastNameCol.setMinWidth(100); | |
lastNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("lastName")); | |
lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
lastNameCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
(t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setLastName(t.getNewValue()); | |
} | |
} | |
); | |
TableColumn emailCol = new TableColumn("Email"); | |
emailCol.setMinWidth(200); | |
emailCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("email")); | |
emailCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
emailCol.setOnEditCommit( | |
new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
(t.getTableView().getItems().get( | |
t.getTablePosition().getRow()) | |
).setEmail(t.getNewValue()); | |
} | |
} | |
); | |
TableColumn<Person, Date> birhdayCol = new TableColumn<>("Birthday"); | |
birhdayCol.setMinWidth(200); | |
birhdayCol.setCellValueFactory( | |
new PropertyValueFactory<Person, Date>("birthday")); | |
birhdayCol.setCellFactory(TextFieldTableCell.<Person, Date>forTableColumn(new DateTimeStringConverter("dd.MM.yyyy HH:mm:ss"))); | |
Callback<TableColumn, TableCell> cellFactory = new TableCallback(); | |
firstNameCol.setCellFactory(cellFactory); | |
lastNameCol.setCellFactory(cellFactory); | |
emailCol.setCellFactory(cellFactory); | |
table.setItems(data); | |
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, birhdayCol); | |
firstNameField = new TextField(); | |
firstNameField.setPromptText("First Name"); | |
firstNameField.setMaxWidth(firstNameCol.getPrefWidth()); | |
lastNameField = new TextField(); | |
lastNameField.setMaxWidth(lastNameCol.getPrefWidth()); | |
lastNameField.setPromptText("Last Name"); | |
emailField = new TextField(); | |
emailField.setMaxWidth(emailCol.getPrefWidth()); | |
emailField.setPromptText("Email"); | |
final Button addButton = new Button("Save"); | |
addButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent e) { | |
if (person == null) { | |
data.add(new Person( | |
firstNameField.getText(), | |
lastNameField.getText(), | |
emailField.getText(), | |
new Date())); | |
firstNameField.clear(); | |
lastNameField.clear(); | |
emailField.clear(); | |
} else { | |
person.setEmail(emailField.getText()); | |
person.setFirstName(firstNameField.getText()); | |
person.setLastName(lastNameField.getText()); | |
person.setBirthday(new Date()); | |
} | |
} | |
}); | |
hb.getChildren().addAll(firstNameField, lastNameField, emailField, addButton); | |
hb.setSpacing(3); | |
final VBox vbox = new VBox(); | |
vbox.setSpacing(5); | |
vbox.setPadding(new Insets(10, 0, 0, 10)); | |
vbox.getChildren().addAll(label, table, hb); | |
((Group) scene.getRoot()).getChildren().addAll(vbox); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private class TableCallback implements Callback<TableColumn, TableCell> { | |
@Override | |
public TableCell call(TableColumn tableColumn) { | |
TableCell cell = new TableCell() { | |
@Override | |
public void updateItem(Object item, boolean empty) { | |
super.updateItem(item, empty); | |
setText(empty ? null : getString()); | |
setGraphic(null); | |
} | |
private String getString() { | |
return getItem() == null ? "" : getItem().toString(); | |
} | |
}; | |
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new TableMouseEventHandler()); | |
return cell; | |
} | |
} | |
private class TableMouseEventHandler implements EventHandler<MouseEvent> { | |
@Override | |
public void handle(MouseEvent event) { | |
if (event.getButton().compareTo(MouseButton.PRIMARY) == 0) { | |
TableCell selectedCell = (TableCell) event.getSource(); | |
person = (Person) selectedCell.getTableRow().getItem(); | |
if (person != null) { | |
firstNameField.setText(person.getFirstName()); | |
lastNameField.setText(person.getLastName()); | |
emailField.setText(person.getEmail()); | |
} else { | |
person = null; | |
firstNameField.clear(); | |
lastNameField.clear(); | |
emailField.clear(); | |
} | |
} | |
} | |
} | |
public static class Person { | |
private final SimpleStringProperty firstName; | |
private final SimpleStringProperty lastName; | |
private final SimpleStringProperty email; | |
private final SimpleObjectProperty<Date> birthday; | |
private Person(String fName, String lName, String email, Date birthday) { | |
this.firstName = new SimpleStringProperty(fName); | |
this.lastName = new SimpleStringProperty(lName); | |
this.email = new SimpleStringProperty(email); | |
this.birthday = new SimpleObjectProperty<>(birthday); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String fName) { | |
firstName.set(fName); | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String fName) { | |
lastName.set(fName); | |
} | |
public String getEmail() { | |
return email.get(); | |
} | |
public void setEmail(String fName) { | |
email.set(fName); | |
} | |
public Date getBirthday() { | |
return birthday.get(); | |
} | |
public SimpleObjectProperty<Date> birthdayProperty() { | |
return birthday; | |
} | |
public void setBirthday(Date birthday) { | |
this.birthday.set(birthday); | |
} | |
public SimpleStringProperty firstNameProperty() { | |
return firstName; | |
} | |
public SimpleStringProperty lastNameProperty() { | |
return lastName; | |
} | |
public SimpleStringProperty emailProperty() { | |
return email; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment