1 pièce(s) jointe(s)
Soucis de rafraîchissement de TableView ?
Bonjour, je développe une petite application de gestion pour m’entraîner. J'ai essayé d'y intégrer une fonction pour faire une genre de recherche filtrée dans ma base de donnée mais il y a l'air d'y avoir un soucis (je pense) au niveau du rafraîchissement du TableView.
Je tiens à préciser que ma BDD est correcte, sur et certain, car les autres fonctionnalités de mon programme sont OK.
Voici à quoi cela ressemble :
Pièce jointe 399564
Voici le fichier FXML (créer avec SceneBuilder) :
Code:
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
|
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="535.0" prefWidth="992.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ControllerRechercherView">
<children>
<Label layoutX="14.0" layoutY="24.0" prefHeight="18.0" prefWidth="174.0" text="Titre" />
<Label layoutX="14.0" layoutY="98.0" prefHeight="18.0" prefWidth="174.0" text="Interprète / Réalisateur" />
<Label layoutX="14.0" layoutY="172.0" prefHeight="18.0" prefWidth="174.0" text="Type" />
<Label layoutX="14.0" layoutY="252.0" prefHeight="18.0" prefWidth="174.0" text="Année de productions" />
<Label layoutX="14.0" layoutY="326.0" prefHeight="18.0" prefWidth="174.0" text="Note ( /5)" />
<TextField fx:id="titre" layoutX="14.0" layoutY="54.0" promptText="Titre" />
<TextField fx:id="interpreterealisateur" layoutX="14.0" layoutY="127.0" promptText="Interprète / Réalisateur" />
<ComboBox fx:id="type" layoutX="14.0" layoutY="200.0" prefHeight="26.0" prefWidth="189.0" promptText="Type" />
<TextField fx:id="anneedeproduction" layoutX="14.0" layoutY="279.0" promptText="Année de production" />
<TextField fx:id="note" layoutX="14.0" layoutY="353.0" promptText="Note ( /5)" />
<Button layoutX="13.0" layoutY="447.0" mnemonicParsing="false" onAction="#BtnOnClick" prefHeight="39.0" prefWidth="190.0" text="Rechercher" />
<Label fx:id="nbresult" layoutX="750.0" layoutY="504.0" prefHeight="18.0" prefWidth="174.0" />
<TableView fx:id="tableprod" layoutX="218.0" layoutY="26.0" prefHeight="474.0" prefWidth="759.0">
<columns>
<TableColumn fx:id="titreColumn" prefWidth="154.39998817443848" text="Titre" />
<TableColumn fx:id="interpreterealisateurColumn" prefWidth="177.60003662109375" text="Interprète / Réalisateur" />
<TableColumn fx:id="typeColumn" prefWidth="121.5999755859375" text="Type" />
<TableColumn fx:id="anneeprodColumn" prefWidth="200.79998779296875" text="Année de production" />
<TableColumn fx:id="noteColumn" prefWidth="83.19992675781248" text="Note ( /5)" />
</columns>
</TableView>
</children>
</AnchorPane> |
Et voici son controller :
Code:
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Thibault Dereumaux
*/
public class ControllerRechercherView {
@FXML
private TableView tableprod;
@FXML
private TextField titre;
@FXML
private TextField interpreterealisateur;
@FXML
private ComboBox<String> type;
@FXML
private TextField anneedeproduction;
@FXML
private TextField note;
@FXML
private Label nbresult;
@FXML
public void initialize() {
type.getItems().removeAll(type.getItems());
type.getItems().addAll("Musique", "Film");
}
private ObservableList<ObservableList> data;
@FXML
private TableColumn<ObservableList, String> titreColumn;
@FXML
private TableColumn<ObservableList, String> interpreterealisateurColumn;
@FXML
private TableColumn<ObservableList, String> typeColumn;
@FXML
private TableColumn<ObservableList, String> anneeprodColumn;
@FXML
private TableColumn<ObservableList, String> noteColumn;
Statement stmtListe;
Connexion maConnexion = new Connexion();
@FXML
void BtnOnClick(ActionEvent event) {
String Titre = titre.getText();
String Interpreterealisateur = interpreterealisateur.getText();
String Type = (String)type.getValue();
String anneeProduction = anneedeproduction.getText();
String Note = note.getText();
System.out.println(Type);
String requeteRecherche;
int noteInt = 0;
int anneeProductionInt = 0;
boolean FormatOk = true;
int nbResultats = 0;
Calendar c = Calendar.getInstance();
if(!Note.isEmpty()) {
try {
noteInt = Integer.decode(Note);
}
catch (NumberFormatException ex) {
System.err.println(ex);
FormatOk = false;
}
}
if(!anneeProduction.isEmpty()) {
try {
anneeProductionInt = Integer.decode(anneeProduction);
}
catch (NumberFormatException ex) {
System.err.println(ex);
FormatOk = false;
}
}
if((noteInt < 0 || noteInt > 5 || anneeProductionInt > c.get(Calendar.YEAR) || anneeProductionInt < -1500) && (!Note.isEmpty() || !anneeProduction.isEmpty())) {
FormatOk = false;
}
if (FormatOk == false) {
nbresult.setText("");
titre.setText("");
interpreterealisateur.setText("");
type.setValue("Type");
anneedeproduction.setText("");
note.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Alerte");
alert.setHeaderText(null);
alert.setContentText("Erreur d'insertion !");
alert.showAndWait();
}
else {
requeteRecherche = "select * from productions where 1=1 ";
if(!Titre.isEmpty()) {
requeteRecherche += "and titre like '"+Titre+"%'";
}
if(!Interpreterealisateur.isEmpty()) {
requeteRecherche += "and interpreterealisateur like '"+Interpreterealisateur+"%'";
}
if("Musique".equals(Type) || "Film".equals(Type)) {
requeteRecherche += "and type like '"+Type+"%'";
}
if(!anneeProduction.isEmpty()) {
requeteRecherche += "and annee_production like '"+anneeProduction+"%'";
}
if(!Note.isEmpty()) {
requeteRecherche += "and note like '"+Note+"%'";
}
try {
stmtListe = (Statement) maConnexion.ObtenirConnection().createStatement();
ResultSet rs = stmtListe.executeQuery(requeteRecherche);
while(rs.next()) {
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
data.add(row);
nbResultats++;
}
tableprod.setItems(data);
titreColumn.setCellValueFactory(feature -> {
final ObservableList list = feature.getValue();
final String title = (String)list.get(1);
return new SimpleStringProperty(title);
});
interpreterealisateurColumn.setCellValueFactory(feature -> {
final ObservableList list = feature.getValue();
final String title = (String)list.get(2);
return new SimpleStringProperty(title);
});
typeColumn.setCellValueFactory(feature -> {
final ObservableList list = feature.getValue();
final String title = (String)list.get(3);
return new SimpleStringProperty(title);
});
anneeprodColumn.setCellValueFactory(feature -> {
final ObservableList list = feature.getValue();
final String title = (String)list.get(4);
return new SimpleStringProperty(title);
});
noteColumn.setCellValueFactory(feature -> {
final ObservableList list = feature.getValue();
final String title = (String)list.get(5);
return new SimpleStringProperty(title);
});
}
catch(SQLException ex) {
System.out.println(ex);
}
System.out.println(nbResultats);
if(nbResultats==0) {
nbresult.setText("");
titre.setText("");
interpreterealisateur.setText("");
type.setValue("Type");
anneedeproduction.setText("");
note.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Alerte");
alert.setHeaderText(null);
alert.setContentText("Aucun résultat !");
alert.showAndWait();
}
else {
if(nbResultats == 1) nbresult.setText(nbResultats + " résultat");
else nbresult.setText(nbResultats + " résultats");
}
}
}
} |
Quand je remplis les champs avec des valeurs qui ne trouveront aucun résultat dans ma BDD, j'ai bien le message correspondant qui s'affiche lorsque je clique sur le bouton, donc c'est OK. Par contre, avec des valeurs qui devraient me trouver des résultats, rien de s'affiche dans ma TableView et j'ai toutes ces erreurs :
Code:
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
|
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at ControllerRechercherView.BtnOnClick(ControllerRechercherView.java:162)
... 58 more |
Merci beaucoup pour votre aide!