dimanche 25 décembre 2016

javafx populate dynamic tableview from database with checkbox

I want to populate a dynamic tableview from database with 32 columns,First column contain the name of employee and remaining 31 columns for marking employee attendence from day1 to day31(checkbox).But I can able to populate tableview from database with 2 columns(name,checkbox) using get and set methods.Here is my code

    String sql ="SELECT * FROM attendence";
    pst =   (PreparedStatement) con.prepareStatement(sql);
    rs=pst.executeQuery();

        while (rs.next()) {
            //get string from db,whichever way 
            String name=rs.getString(3);
            int day=rs.getInt(6); 
            data.add(new User(name,day!=0)); //converting integer to boolean and storing on data(Observable list)
        }

         etname.setCellValueFactory(new PropertyValueFactory<>("name"));
         col.setCellValueFactory(new PropertyValueFactory<>("day1));

         col.setCellFactory(new Callback<TableColumn<User, Boolean>,       TableCell<User, Boolean>>() {

         public TableCell<User, Boolean> call(TableColumn<User, Boolean> p) {

            return new CheckBoxTableCell<User, Boolean>();

        }
          });

           jTable.getColumns().add(etname,col);
            jTable.setItems(data);
             }

This is User.java

   public class User {


      private final SimpleStringProperty ename;
      private BooleanProperty day1;

      User(String Ename,boolean day1)   
      {
        this.ename = new SimpleStringProperty(Ename);
        this.day1 = new SimpleBooleanProperty(day1);

        this.day1.addListener(new ChangeListener<Boolean>() {

            public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {

                System.out.println(enameProperty().get() + " invited: " + t1);
                System.out.println();
            }

        });            

    }


    public String getEname() {
        return ename.get();
      }


    public void setEname(String Ename) {
    ename.set(Ename);
   }


   public BooleanProperty day1Property() {
     return day1; 
    }


  public StringProperty enameProperty() {
    return ename;
    }

CheckBoxTableCell.java

 public class CheckBoxTableCell<S, T> extends TableCell<S, T> {

    private final CheckBox checkBox;

    private ObservableValue<T> ov;



    public CheckBoxTableCell() {

        this.checkBox = new CheckBox();

        this.checkBox.setAlignment(Pos.CENTER);



        setAlignment(Pos.CENTER);

        setGraphic(checkBox);

    } 



    @Override public void updateItem(T item, boolean empty) {

        super.updateItem(item, empty);

        if (empty) {

            setText(null);

            setGraphic(null);

        } else {

            setGraphic(checkBox);

            if (ov instanceof BooleanProperty) {

                checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);

            }

            ov = getTableColumn().getCellObservableValue(getIndex());

            if (ov instanceof BooleanProperty) {

                checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);

            }

        }

    }

}

But no idea how i can do with 32 columns. So i need a big help from anyone for my 2 problems. 1) How i can populate dynamic tableview from database with checkboxes 2) when i press a button should read all the names along with checkbox status(isSelected or not selected) like

  • jhon true true false ... ....
  • rose false false true ... ..

  • george true true true false

Answers will be appreciated.Thank you in advance. .. ..




Aucun commentaire:

Enregistrer un commentaire