[英]Can't set different values for my TableColumn in JavaFX
我試圖在TableView
列中設置一個Button
,但未成功設置其他按鈕。 它在所有列上顯示相同的按鈕。
這個想法是正確設置名稱的,但是我的問題是當我設置cellValueFactory
。 這是我的代碼:
for (int i = 0; i < listaPatchuriPerBaza.length; i++) {
if (listaPatchuriPerBaza[i].contains(".sql")) {
k = i;
pozitie = poz;
Patch pt = new Patch(listaPatchuriPerBaza[i], "Run" + " " + listaPatchuriPerBaza[i]);
listaPatchuri.add(pt);
masterData.add(pt);
patchColumn.setCellValueFactory(new PropertyValueFactory<Patch, String>("denumire"));
runColumn.setCellValueFactory(new PropertyValueFactory<Patch, String>("btnText"));
runColumn.setCellFactory(new Callback<TableColumn<Patch, String>, TableCell<Patch, String>>() {
@Override
public TableCell<Patch, String> call(TableColumn<Patch, String> param) {
Button btn = new Button(pt.getBtnText());
//Set up the Table
TableCell<Patch, String> cell = new TableCell<Patch, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
//actioneaza butonul de run
btn.setOnAction((ActionEvent event) -> {
btn.setDisable(true);
Tooltip tool = new Tooltip("Running");
setTooltip(tool);
System.out.println("ruleaza");
try {
SmbFile script = new SmbFile(path + pt.getDenumire(), userCred);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
SmbFile smbFromFile = new SmbFile(path + pt.getDenumire(), userCred);
SmbFile smbToFile = new SmbFile(path + "Aplicate/" + pt.getDenumire(), userCred);
smbFromFile.renameTo(smbToFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
setGraphic(btn);
}
}
};
return cell;
}
});
}
tableView.setItems(masterData);
}
為表中的每一行設置一次單元格值工廠和一次單元格工廠是沒有意義的:您要做的就是每次迭代循環時都替換工廠。 這些是工廠 :即創建其他對象的對象。 table列將根據需要調用其call
方法以創建多個單元格等。
因此,只需設置一次並在updateItem()
方法中更新按鈕的文本updateItem()
。 該按鈕必須是單元格的屬性(以便每個單元格都有自己的按鈕,帶有自己的文本),而不是單元格工廠的屬性(因為整個列只有一個單元格工廠)。
runColumn.setCellValueFactory(new PropertyValueFactory<Patch, String>("btnText"));
runColumn.setCellFactory(column -> new TableCell<Patch, String>() {
Button btn = new Button();
{
btn.setOnAction(e -> {
Patch pt = getTableView().getItems().get(getIndex());
btn.setDisable(true);
Tooltip tool = new Tooltip("Running");
setTooltip(tool);
System.out.println("ruleaza");
try {
SmbFile script = new SmbFile(path + pt.getDenumire(), userCred);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
SmbFile smbFromFile = new SmbFile(path + pt.getDenumire(), userCred);
SmbFile smbToFile = new SmbFile(path + "Aplicate/" + pt.getDenumire(), userCred);
smbFromFile.renameTo(smbToFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setGraphic(null);
} else {
btn.setText(item);
setGraphic(btn);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.