0

I'm new in JavaFX. I'm trying to populate a table from a mysql database. I get all the records, but i can't display them in table.

Here is my code for model class:

package pos_fxml;

import javafx.beans.property.*;    

public class PosForTable {
    
    private final IntegerProperty id;
    private final StringProperty nume;
    private final StringProperty tip;
    private final BooleanProperty activ;

    public PosForTable(int iId, String sNumeRo, String sTipRo, boolean bActiv) {
        this.id = new SimpleIntegerProperty(iId);
        this.nume = new SimpleStringProperty(sNumeRo);
        this.tip = new SimpleStringProperty(sTipRo);
        this.activ = new SimpleBooleanProperty(bActiv);            
    }

    public int getid() {
        return id.get();
    }

    public void setid(int iId) {
        id.set(iId);
    }

    public String getnume() {
        return nume.get();
    }

    public void setnume(String sNumeRo) {
        nume.set(sNumeRo);
    }

    public String gettip() {
        return tip.get();
    }

    public void settip(String sTipRo) {
        tip.set(sTipRo);
    }

    public String getactiv() {
        String sActiv = "";
        if (activ.get()){
            sActiv = "Activ";
        }
        else{
            sActiv = "Inactiv";
        }
        return sActiv;
    }

    public void setactiv(String sActiv) {
        if (sActiv.equals("Activ")){
            activ.set(true);;
        }else{
            activ.set(false);;
        }
    }
}

and the fxml:

package pos_fxml;

import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
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.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class FXMLDocumentController implements Initializable {        
    @FXML
    private Label label;
    @FXML
    private Button btAdd;
    @FXML
    private TableView<PosForTable> table;
    @FXML
    private TableColumn<PosForTable, Integer> colID;
    @FXML
    private TableColumn<PosForTable, String> colNume;
    @FXML
    private TableColumn<PosForTable, String> colTip;
    @FXML    
    private TableColumn<PosForTable, Boolean> colAct;
    
    private ObservableList<PosForTable>data;
    private DbConnection dc;
    
        private void handleButtonAction(ActionEvent event) {
        
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        dc = new DbConnection();
        loadDataFromDatabase();
    }
    
        private void loadDataFromDatabase(){
        try {
            Connection conn = dc.Connect();
            data = FXCollections.observableArrayList();
            ResultSet rs = conn.createStatement().executeQuery("SELECT pct_de_vanz.id,pct_de_vanz.nume_ro, tip_pv.tip_ro, pct_de_vanz.activ FROM pct_de_vanz JOIN tip_pv ON (pct_de_vanz.tip_pv = tip_pv.id)");
            
            while (rs.next()){                
                data.add(new PosForTable(rs.getInt("id"),rs.getString("nume_ro"),rs.getString("tip_ro"),rs.getBoolean("activ")));                
            }            
            
        } catch (SQLException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Error"+ex);            
        }
        
        colID.setCellValueFactory(new PropertyValueFactory<PosForTable, Integer>("id"));        
        colNume.setCellValueFactory(new PropertyValueFactory<PosForTable, String>("nume"));
        colTip.setCellValueFactory(new PropertyValueFactory<PosForTable, String>("tip"));        
        colAct.setCellValueFactory(new PropertyValueFactory<PosForTable, Boolean>("activ"));
        table.setItems(null);
        table.setItems(data);
    }    
}

Finally I can see 15 records in the table, but no content.

Here is the result:

enter image description here

Thank you for your help in advance.

James_D
  • 188,960
  • 15
  • 271
  • 305

0 Answers0