lundi 24 juillet 2017

How to put checkboxes in TableView and make them selected when searching from database in javafx

My database has 5 fields (UserName,Password,RePassword,Admin,NormalUser) based on my database fields I created a form containing a Tableview with UserName,Password,RePassword,Admin,NormalUser columns, successfully. Admin and Username are Boolean because I want to make difference between users type. When I fetched data from the database I could see the words "False" and "True" but I wanna show them with checkboxes in the Admin and NormalUser column. I don't have any idea about how to code this part. I searched through the Internet and I found a code to create a TableView dynamically (all of the columns have String type)but I coulde'nt modify it to have three column with String type and two column with checkboxes(Boolean Type) and made them selected after fetching these two column data.

import Scheduling.ConnectDBwithFunc;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.NodeOrientation;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.util.Callback;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;


class FillTableofUsers{

//TABLE VIEW AND DATA
private ObservableList<ObservableList> data;


//CONNECTION DATABASE
public TableView buildData(){
    Connection c = null;
    TableView tableview = new TableView();
    tableview.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
    data = FXCollections.observableArrayList();
    try{

        c = ConnectDBwithFunc.connect(c);
        String SQL = "SELECT USERNAME, PASSWORD ,REPASSWORD, ADMIN , 
    NORMALUSER from USERS";

        //ResultSet
        ResultSet rs = c.createStatement().executeQuery(SQL);

        /**********************************
         * TABLE COLUMN ADDED DYNAMICALLY *
         **********************************/

        ArrayList<String> tableTitr = new ArrayList<>
(Arrays.asList("UserName","Password"," RePassword" , "Password" 
,"Admin","NormalUser"));
        for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){

            //We are using non property style for making dynamic table
            final int j = i;
            TableColumn col = new TableColumn(tableTitr.get(i));

            col.setCellValueFactory(new 
Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });

            tableview.getColumns().addAll(col);
        }

        /********************************
         * Data added to ObservableList *
         ********************************/
        String[] rowInfo = new String[rs.getMetaData().getColumnCount()+1];
        while(rs.next()){
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
                //Iterate Column
                if (rs.getString(i)!=null)
                    rowInfo[i] = rs.getString(i);


                else
                    rowInfo[i] = " ";

                row.add(rowInfo[i]);
            }

            data.add(row);
        }

        //FINALLY ADDED TO TableView
        tableview.setItems(data);

    }catch(Exception e){
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }


    return tableview;
}

}




Aucun commentaire:

Enregistrer un commentaire