有条件向javafx表视图添加按钮

时间:2019-03-04 13:13:49

标签: java javafx

我是javafx的新手,正在努力将Button添加到带有条件的TableView中。

如果条件为true,则将添加按钮,如果为false,则按钮将不会添加到tableView。我用谷歌搜索,但没有找到任何建议或解决方案。   有什么方法可以实现它。预先感谢。

这是我的控制器

@FXML
    TableView<Employee> employeeTable;
    @FXML
    TableColumn<Employee, Integer> col_id;
    @FXML
    TableColumn<Employee, String> col_fatherName;
    @FXML
    TableColumn<Employee, String> col_CNIC;
    @FXML
    TableColumn<Employee, String> col_gender;
    @FXML
    TableColumn<Employee, Button> update;


    List<Employee> employees = new ArrayList<>();
    ObservableList<Employee> obs = FXCollections.observableArrayList();
  private Employee data;


 @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO

        setColumnProperties();
        addButtonToTable();
        addDeleteButton();
        loadData();
}


  private void loadData() {


        employees = employeeDAO.findAllEmployees();


        obs = FXCollections.observableArrayList(employees);

        employeeTable.getItems().clear();
        employeeTable.getItems().addAll(obs);


    }


    public void setColumnProperties() {

        col_id.setCellValueFactory(new PropertyValueFactory<Employee, Integer>("id"));
        col_fatherName.setCellValueFactory(new PropertyValueFactory<Employee, String>("fatherName"));
        col_CNIC.setCellValueFactory(new PropertyValueFactory<Employee, String>("cnic"));
        col_gender.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));

    }


      private void addButtonToTable() {


        Callback<TableColumn<Employee, Button>, TableCell<Employee, Button>> cellFactory = new Callback<TableColumn<Employee, Button>, TableCell<Employee, Button>>() {
            @Override
            public TableCell<Employee, Button> call(final TableColumn<Employee, Button> param) {
                final TableCell<Employee, Button> cell = new TableCell<Employee, Button>() {
                    Image imgEdit = new Image(getClass().getResourceAsStream("/images/download.png"));
                    {
}
                    @Override
                    public void updateItem(Button item, boolean empty) {
                        super.updateItem(item, empty);
                    edite=new Button("Btn");  // global btn
                        if (empty) {
                            setGraphic(null);

                        } else {

                            // here i am trying to write condition

                          Iterator ite=  employeeDAO.findAllEmployees().iterator();
                            while (ite.hasNext()){
                             Employee employee=(Employee)   ite.next();
                                if (employee.getName().equals("jnk"))
                                {


                            edite.setOnAction((ActionEvent event) -> {
                                System.out.println( edite.getId());
                                data = getTableView().getItems().get(getIndex());
                                fatherName.setText(data.getFatherName());
                                CNIC.setText(data.getCnic());
                                name.setText(data.getName());
                                register_btn.setText("Update");


                            });
                            edite.setStyle("-fx-background-color: transparent;");
                            ImageView iv = new ImageView();
                            iv.setFitHeight(50);
                            iv.setFitWidth(50);
                            iv.setImage(imgEdit);
                            iv.setPreserveRatio(true);
                            iv.setSmooth(true);
                            iv.setCache(true);
                           edite.setGraphic(iv);
                            setGraphic(edite);
                            setText(null);

                                }
                                else{

                                    System.out.println("not working");
                                }
                            }


                        }

                    }
                };
                return cell;
            }

            ;
        };


update.setCellFactory(cellFactory);
       employeeTable.getColumns().add(update);

        register_btn.setText("Register");

    }

我的目标是如果Name ='jnk'添加按钮 但条件为真后,将其添加到所有行中。

我的员工班级

public class EMployee{
    private int id;
    private String name;
    private String cnic;
    private String skill;
    private String dob;
    private String fatherName;
    private String gender;
    private int  value;

setter and getter
} 

EmployeeDao类为

Public Class EmployeeDAO extends JdbcDaoSupport {

    public List<Employee> findAllEmployees() {

        List<Employee> empList = new ArrayList<>();
        String query = "select * from employee";
         getJdbcTemplate().query(query,
                new BeanPropertyRowMapper<Employee>(Employee.class));
        return empList;
    }

}

0 个答案:

没有答案