JavaFX : Changement de panel et affichage d’une liste d’objets
Intro
- Votre travail sur votre MyEasyline continue ;-)
Etape 1 Sélection de la liste des voyageurs avec JDBC
Livrable : Easyline/mission9/…
- Il faut créer une nouvelle fonction de sélection pour récupérer la liste des voyageurs dans VoyageurDAO (côté backend)
public class VoyageurDAO{ public ArrayList<Voyageur> selectALLVoyageurs(){ // code JDBC à compléter // requête sql qui permet de récupérer le voyageur et son adresse postale // retour : liste de Voyageurs qui correspond à tous les objets Voyageurs de la bdd } }
- Tester cette fonction dans un main classique (côté backend, sans Javafx)
- Il faut créer une 2e fonction qui permet de récupérer les voyageurs sans bagage dans la classe VoyageurDAO (côté backend)
public class VoyageurDAO{ public ArrayList<Voyageur> selectALLVoyageurs(){ // code JDBC à compléter // requête sql qui permet de récupérer le voyageur et son adresse postale // retour : liste de Voyageurs qui correspond à tous les objets Voyageurs de la bdd } }
- Tester cette fonction dans un main classique (côté backend, sans Javafx)
Etape 2
Livrable : Easyline/mission9/…
- Créer un panel avec deux boutons dans une fenêtre JavaFX, tel que :
- Le premier bouton : permet d’afficher tous les voyageurs dans un TableView créé dans un deuxième panel
- Le deuxième bouton : permet d’afficher tous les voyageurs sans bagage dans le même tableView
- Le panel du TableView doit ressembler à :
- Compléter le tableView pour afficher la liste des voyageurs.
Voici 2 tutoriels pour vous accompagner :
https://o7planning.org/11079/javafx-tableview ou
https://www.developpez.net/forums/blogs/69141-tondeurh/b1366/exemple-l-utilisation-d-tableview-javafx-fxml/ - …
VoyageurControleur.java
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.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Stage; public class VoyageurListeControleur implements Initializable{ private ObservableList<VoyageurBean> VoyageurData = FXCollections.observableArrayList(); @FXML private Button button; @FXML private TableView<VoyageurBean> TVVoyageur; @FXML private TableColumn<VoyageurBean, String> colonneNom; @FXML private TableColumn<VoyageurBean, String> colonneAge; @FXML private void handleButtonAction(ActionEvent event) { stage.close(); } @Override public void initialize(URL url, ResourceBundle rb) { // TODO VoyageurData.add(new VoyageurBean("Joe","23")); VoyageurData.add(new VoyageurBean("Henri","12")); VoyageurData.add(new VoyageurBean("Druss","25")); VoyageurData.add(new VoyageurBean("Daril","4")); colonneNom.setCellValueFactory(new PropertyValueFactory<>("nom")); colonneAge.setCellValueFactory(new PropertyValueFactory<>("age")); TVVoyageur.setItems(VoyageurData); } Stage stage; void setStage(Stage stg){ stage=stg; } }
VoyageurListe.fxml
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (c) 2015, 2019, Gluon and/or its affiliates. All rights reserved. Use is subject to license terms. This file is available and licensed under the following license: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Oracle Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SeparatorMenuItem?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.VBox?> <?import javafx.scene.text.Font?> <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="VoyageurListeControleur"> <children> <MenuBar VBox.vgrow="NEVER"> <menus> <Menu mnemonicParsing="false" text="File"> <items> <MenuItem mnemonicParsing="false" text="New" /> <MenuItem mnemonicParsing="false" text="Open…" /> <Menu mnemonicParsing="false" text="Open Recent" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Close" /> <MenuItem mnemonicParsing="false" text="Save" /> <MenuItem mnemonicParsing="false" text="Save As…" /> <MenuItem mnemonicParsing="false" text="Revert" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Preferences…" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Quit" /> </items> </Menu> <Menu mnemonicParsing="false" text="Edit"> <items> <MenuItem mnemonicParsing="false" text="Undo" /> <MenuItem mnemonicParsing="false" text="Redo" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Cut" /> <MenuItem mnemonicParsing="false" text="Copy" /> <MenuItem mnemonicParsing="false" text="Paste" /> <MenuItem mnemonicParsing="false" text="Delete" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Select All" /> <MenuItem mnemonicParsing="false" text="Unselect All" /> </items> </Menu> <Menu mnemonicParsing="false" text="Help"> <items> <MenuItem mnemonicParsing="false" text="About MyHelloApp" /> </items> </Menu> </menus> </MenuBar> <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> <children> <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style=" " text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false"> <font> <Font size="18.0" /> </font> </Label> <TableView fx:id="TVVoyageur" layoutX="184.0" layoutY="9.0" prefHeight="352.0" prefWidth="341.0"> <columns> <TableColumn fx:id="colonneNom" prefWidth="183.0" text="nom" /> <TableColumn fx:id="colonneAge" minWidth="0.0" prefWidth="157.0" text="age" /> </columns> </TableView> </children> </AnchorPane> </children> </VBox>
Main.java
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; public class Main extends Application { public static void main(String args[]){ launch(args); } @Override public void start(Stage panel) throws Exception { // TODO Auto-generated method stub /*Parent root = FXMLLoader.load(getClass().getResource("voyageurListe.fxml")); Scene scene = new Scene(root,600,300); panel.setTitle("waza"); panel.setScene(scene); panel.show();*/ FXMLLoader loader = new FXMLLoader(Main.class.getResource("voyageurListe.fxml")); Parent root = (Parent)loader.load(); final VoyageurListeControleur controller = loader.getController(); controller.setStage(panel); Scene scene = new Scene(root,600,300); panel.setScene(scene); panel.setTitle("waza"); panel.show(); } }
VoyageurBean.java
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; public class VoyageurBean { public StringProperty nom; public StringProperty age; public VoyageurBean(String nom, String age){ this.nom=new SimpleStringProperty(nom); this.age=new SimpleStringProperty(String.valueOf(age)); } public StringProperty getAge() { return age; } public StringProperty getNom() { return nom; } public void setAge(StringProperty age) { this.age = age; } public void setNom(StringProperty nom) { this.nom = nom; } public StringProperty nomProperty() { return nom; } public StringProperty ageProperty() { return age; } }