1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
public class Main extends Application {
private TableView<Person> table = new TableView<Person>();
public static void main(String[] args) {
launch(args);
}
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Albert", "Camus"),
new Person("Yannick", "Noah"),
new Person("Charles", "Aznavour")
);
@Override
public void start(Stage stage) {
table.setPrefWidth(550);
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(600);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
//table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
System.out.println("avant table.getColumns()");
table.getColumns().addAll(firstNameCol, lastNameCol);
table.setItems(data);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person,String>("firstName")
);
System.out.println("avant lastNameCol.setCellValueFactory");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person,String>("lastName")
);
firstNameCol.setCellFactory(new Callback<TableColumn<Person,String>, TableCell<Person,String>>() {
@Override
public TableCell<Person,String> call(TableColumn<Person,String> param) {
return new TableCell<Person,String>(){
@Override
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if(!empty){
System.out.println("passe dans setCellFactory : " + item);
setText(item);
}
}// du protected
};// du return
}
});//
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
} |