par , 22/05/2016 à 11h37 (7472 Affichages)
Vous trouverez ci dessous un exemple de programme mettant en scène le composant TableView.
IL est important de remarquer l’intérêt des tags <cellValueFactory><PropertyValueFactory property= "nom"/></cellValueFactory> affecté a chaque TableColumn
<TableColumn prefWidth= »135.0″ text= »Nom »>
<cellValueFactory><PropertyValueFactory property= "nom"/></cellValueFactory>
</TableColumn>

Ci dessous le Bean Person.java qui va nous servir pour afficher les données d'une personne dans notre TableView
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
|
package javafxgridview;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
public StringProperty nom;
public StringProperty prenom;
public Person() {
this.nom = null;
this.prenom = null;
}
public Person(String nom, String prenom) {
this.nom = new SimpleStringProperty(nom);
this.prenom = new SimpleStringProperty(prenom);
}
/**
* @return the nom
*/
public String getNom() {
return this.nom.get();
}
/**
* @param nom the nom to set
*/
public void setNom(String nom) {
this.nom.set(nom);
}
/**
* @return the prenom
*/
public String getPrenom() {
return this.prenom.get();
}
/**
* @param prenom the prenom to set
*/
public void setPrenom(String prenom) {
this.prenom.set(prenom);
}
} |
Le fichier fxml, FXMLGridView.fxml,
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
|
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="402.0" prefWidth="323.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxgridview.FXMLGridViewController">
<children>
<Button fx:id="button" layoutX="244.0" layoutY="363.0" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="69.0" text="Bye!" />
<TableView fx:id="TVPerson" layoutX="23.0" layoutY="21.0" prefHeight="334.0" prefWidth="278.0" AnchorPane.bottomAnchor="47.0" AnchorPane.leftAnchor="23.0" AnchorPane.rightAnchor="22.0" AnchorPane.topAnchor="21.0">
<columns>
<TableColumn prefWidth="135.0" text="Nom">
<cellValueFactory><PropertyValueFactory property="nom"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="0.0" prefWidth="142.0" text="Prenom">
<cellValueFactory><PropertyValueFactory property="prenom"/></cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</AnchorPane> |
Le fichier application principale JavaFxGridView.java
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
|
package javafxgridview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXGridView extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(JavaFXGridView.class.getResource("FXMLGridView.fxml"));
Parent root = (Parent)loader.load();
final FXMLGridViewController controller = loader.getController();
controller.setStage(stage);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Exemple d'utilsation d'un tabview - interêt du CellValueFactory");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
} |
Le fichier controller, FXMLGridViewController.java
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
| package javafxgridview;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class FXMLGridViewController implements Initializable {
private ObservableList<Person> PersonData = FXCollections.observableArrayList();
@FXML
private Button button;
@FXML
private TableView<Person> TVPerson;
@FXML
private void handleButtonAction(ActionEvent event) {
stage.close();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
PersonData.add(new Person("Reese","Kyle"));
PersonData.add(new Person("Smith","John"));
PersonData.add(new Person("Wright","Marcus"));
PersonData.add(new Person("Connor","Sarah"));
TVPerson.getItems().addAll(PersonData);
}
Stage stage;
void setStage(Stage stg){stage=stg;}
} |
Voila, rien de neuf sous le soleil, mais si cela peut aider quelques développeur en mal de JavaFX de se lancer et éviter de galérer sur ce problème.
FIN