我已经发现如何在行的TableView中插入一个按钮,但我不确定如何为该按钮添加不同的值:
column
.setCellFactory(new Callback<TableColumn<GuiObject, Boolean>, TableCell<GuiObject, Boolean>>() {
@Override
public TableCell<GuiObject, Boolean> call(
TableColumn<GuiObject, Boolean> p) {
return new ButtonCell();
}
});
其中ButtonCell是TableCell<GuiObject, Boolean>
的自实现按钮,但我希望能够根据我插入的行动态插入不同的按钮。
答案 0 :(得分:1)
如果您从java访问TableView,请执行以下操作:
@FXML
TableView myTable;
@Override
public void initialize(URL url, ResourceBundle rb) {
TableColumn<Item, String> firstColumn = new TableColumn<>("First Column");
firstColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Item, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Item, String> p) {
return new ReadOnlyObjectWrapper(p.getValue().column1);
}
});
TableColumn<Item, String> secondColumn = new TableColumn<>("Second Column");
secondColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Item, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Item, String> p) {
return new ReadOnlyObjectWrapper(p.getValue().column2);
}
});
TableColumn<Item, Button> buttonCol = new TableColumn<>("ButtonColumn");
buttonCol.setSortable(false);
buttonCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Item, Button>, ObservableValue<Button>>() {
@Override
public ObservableValue<Button> call(TableColumn.CellDataFeatures<Item, Button> features) {
return new ReadOnlyObjectWrapper(features.getValue().button);
}
});
myTable.getColumns().add(buttonCol);
myTable.getColumns().add(firstColumn);
myTable.getColumns().add(secondColumn);
myTable.getItems().add(new Item("Test 1", "Test 1", new Button("Test 1"), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
//ON ACTION CODE HERE
System.out.println("TEST 1 CLICKED!");
}
}));
myTable.getItems().add(new Item("Test 2", "Test 2", new Button("Test 2"), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
//ON ACTION CODE HERE
System.out.println("TEST 2 BUTTON CLICKED");
}
}));
}
和Item类:
public class Item {
public String column1, column2;
public Button button;
public Item(String column1, String column2, Button b) {
this.column1 = column1;
this.column2 = column2;
button = b;
}
}
证明它有效:
答案 1 :(得分:0)
由于Cobbles的代码,我修复了最后一期。我只是添加了一个方法addAddButton()
,它为新行添加了一个按钮,并为Item类型的现有对象提供了功能:
private void addAddButton() {
Button b = new Button(" + ");
final Item item = new Item(="<new>",
"<new>", b);
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Button deleteButton = new Button(" - ");
deleteButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
selectedItems.remove(item); // a predefined list of selected items
myTable.getItems().clear();
for (Item current : selectedItems) {
myTable.getItems().add(current);
}
addAddButton();
}
});
item.setButton(deleteButton);
selectedItems.add(item);
myTable.getItems().clear();
for (Item current : selectedItems) {
myTable.getItems().add(current);
}
addAddButton();
}
});
tableAvailabilites.getItems().add(ga1);
}
它还没有超级高效,但它正在发挥作用!