无法在 JavaFX 中为我的 TableColumn 设置不同的值
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 中的每一行设置单元格值工厂和单元格工厂一次是没有意义的:您所做的就是每次迭代循环时替换工厂。这些是 工厂:即创建其他对象的对象。 table 列将根据需要调用它们的 call
方法来创建多个单元格等。
因此只需设置一次并在 updateItem()
方法中更新按钮的文本。按钮需要是 cell 的 属性(这样每个单元格都有自己的按钮,有自己的文本),而不是 cell 工厂的(因为整列只有一个cell factory)
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);
}
}
});
我试图在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 中的每一行设置单元格值工厂和单元格工厂一次是没有意义的:您所做的就是每次迭代循环时替换工厂。这些是 工厂:即创建其他对象的对象。 table 列将根据需要调用它们的 call
方法来创建多个单元格等。
因此只需设置一次并在 updateItem()
方法中更新按钮的文本。按钮需要是 cell 的 属性(这样每个单元格都有自己的按钮,有自己的文本),而不是 cell 工厂的(因为整列只有一个cell factory)
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);
}
}
});