【问题标题】:javafx populate dynamic tableview from database with checkboxjavafx 使用复选框从数据库中填充动态表视图
【发布时间】:2016-12-25 13:51:47
【问题描述】:

我想用 32 列从数据库中填充动态表视图,第一列包含员工姓名,其余 31 列用于标记从第 1 天到第 31 天的员工出勤(复选框)。但我可以用 2 从数据库中填充表视图使用 get 和 set 方法的列(名称,复选框)。这是我的 代码

    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);
             }

这是 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);

            }

        }

    }

}

但不知道如何处理 32 列。 所以我需要任何人的大力帮助来解决我的两个问题。 1) 如何使用复选框从数据库中填充动态表格视图2) 当我按下按钮时应该读取所​​有名称以及复选框状态(已选择或未选择),例如

  • jhon true true false ....
  • 玫瑰假假真.....

  • 乔治真真真假

我们将不胜感激。在此先感谢您。 .. ..

【问题讨论】:

    标签: sqlite checkbox tableview javafx-8


    【解决方案1】:

    尝试使用此代码。

      private static final List<String> groups = Arrays.asList("Group 1", "Group 2", "Group 3", "Group 4"); //declared an array
    
      TableView<AttributeRow> attributeTable = new TableView<>(); //new Table
       for (String group : groups) {  //Creating dynamic column
            TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group);
            groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
            groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group));
            attributeTable.getColumns().add(groupColumn);
        }
    

    和 AttributeRow 类

    class AttributeRow {
    
    private final Map<String, BooleanProperty> activeByGroup = new HashMap<>();
    
    
    public AttributeRow(List<String> companyGroups) {
        for (String group : companyGroups) {
    
            activeByGroup.put(group, new SimpleBooleanProperty()) ;
        }
    }
    
    public final BooleanProperty activeProperty(String group) {
    
        return activeByGroup.get(group) ;
    }
    
     public final boolean isActive(String group) {
        return activeProperty(group).get();
    }
    
    public final void setActive(String group, boolean active) {
        activeProperty(group).set(active);
    }
    

    要加载复选框数据(真或假),我们可以使用以下方法

             String sql ="SELECT * FROM attendence";
             pst =   (PreparedStatement) con.prepareStatement(sql);
             rs=pst.executeQuery();
             while (rs.next()) {
    
                   AttributeRow row = new AttributeRow(groups);
    
                   for(int j=0;j<5;j++) //here j<5 because array size =5
                               {
                                   int i=j+6;
                                int day=rs.getInt(i); //getting Integer value(note:sqlite does not support boolean datatype so i stored as integer with 0 or 1)
    
                                row.setActive(day!=0); //converted into boolean useing day!=0
                               }
                                jTable.getItems().add(row);
    
                   }
    

    【讨论】:

    • 哇,工作得很好。但是你从来没有解释过如何从数据库中生成数据
    • 哇,非常感谢它完美运行。你拯救了我的一天
    猜你喜欢
    • 2013-09-01
    • 2017-05-02
    • 1970-01-01
    • 2014-12-14
    • 2022-01-24
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多