Я новичок в javafx и работаю над добавлением Button в TableView с условием.
Если условие истинно, оно добавит кнопку, а если 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");
}
моя цель - добавить кнопку, если имя = '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;
}
}