I know this question has answers here, Still I dont find why my table is not updating values .
Here is my codes...
I have used this class as table data type,
package demo;
public class Doctor {
private String name;
private String dept;
private String salary;
private String phone;
public Doctor() {
}
public Doctor(String n, String d, String s, String p) {
name = n;
dept = d;
salary = s;
phone = p;
}
}
This is my FXML code of The controller class
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="698.0" prefWidth="526.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="demo.DoctorController">
<children>
<AnchorPane layoutX="4.0" layoutY="3.0" prefHeight="498.0" prefWidth="530.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TableView fx:id="doctorsTable" layoutX="10.0" layoutY="70.0" prefHeight="197.0" prefWidth="509.0">
<columns>
<TableColumn fx:id="docNameCol" editable="false" prefWidth="124.0" text="NAME" />
<TableColumn fx:id="docDeptCol" prefWidth="102.0" text="DEPT." />
<TableColumn fx:id="docSalaryCol" prefWidth="98.0" text="SALARY" />
<TableColumn fx:id="docPhoneCol" prefWidth="167.0" text="PHONE" />
</columns>
</TableView>
<Label layoutX="11.0" layoutY="34.0" text="LIST OF ALL THE DOCTORS(S)" />
<ListView fx:id="doctorList" layoutX="11.0" layoutY="363.0" prefHeight="121.0" prefWidth="264.0" />
<Label layoutX="14.0" layoutY="298.0" text="SELECT A DOCTOR TO SEE THEIR APPOINTMENTS WITH THEIR PATIENTS" />
<Label layoutX="14.0" layoutY="325.0" text="LIST OF PATIENTS:" />
<Label layoutX="198.0" layoutY="602.0" text="Label">
<font>
<Font size="72.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</AnchorPane>
And This is my Controller class...
package demo;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListView;
public class DoctorController implements Initializable {
@FXML
private TableView<Doctor> doctorsTable;
@FXML
private TableColumn<Doctor, String> docNameCol;
@FXML
private TableColumn<Doctor, String> docDeptCol;
@FXML
private TableColumn<Doctor, String> docSalaryCol;
@FXML
private TableColumn<Doctor, String> docPhoneCol;
@FXML
private ListView<String> doctorList;
private Connection con;
private Statement stat;
private ResultSet res;
private String query;
private String fname;
private String fdept;
private String fsalary;
private String fphone;
DatabaseConnection dbobj = new DatabaseConnection();
private void connect() throws SQLException{
con=dbobj.getConn();
stat=dbobj.getState();
}
private void init(){
docNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
docDeptCol.setCellValueFactory(new PropertyValueFactory<>("dept"));
docSalaryCol.setCellValueFactory(new PropertyValueFactory<>("salary"));
docPhoneCol.setCellValueFactory(new PropertyValueFactory<>("phone"));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
init();
try {
populateTable();
System.out.println("items added");
} catch (SQLException ex) {
Logger.getLogger(DoctorController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void populateTable() throws SQLException{
ObservableList<Doctor> tableVlues = FXCollections.observableArrayList();
ObservableList<String> list = FXCollections.observableArrayList();
connect();
query = "Select * from Doctor";
res = stat.executeQuery(query);
tableVlues.add(new Doctor());
while(res.next()){
fname = res.getString("doctorName");
fdept = res.getString("doctorDept");
fsalary = res.getString("doctorSalary");
fphone = res.getString("doctorPhone");
System.out.println(fname+", "+fdept+" , "+fsalary+" , "+fphone); // This line prints everything correctly
tableVlues.add(new Doctor(fname, fdept, fsalary, fphone));
list.add(fname+", "+fdept+" , "+fsalary+" , "+fphone);
}
doctorList.setItems(list);
doctorsTable.setItems(tableVlues);
}
}
My Db connection is ok. Beside, I tried to store the fetched items into the ListView. And this works perfectly. But it is not working with the TableView. What am I doing wrong ? Please Help.