问题
The code below are instance methods of an object.
private StringProperty buySell;
// getters
public String getBuySell(){
return this.buySell.get();
}
// return Property Object
public StringProperty buySellProperty(){
return this.buySell;
}
// setters
public void setBuySell(String buySell){
this.buySell.set(buySell);
}
In my Controller
class, I have created a TableColumn
for buySell
string property with the code below.
When I created a transaction, there will be a new row on the tableView
. However, I want to be able to edit the buySell tableCell.
Question: How can I embed a choicebox
with values buy
, sell
within the setOnEdit
function such that when I double click on the cell, it will give me a choicebox ?
I have my choicebox
code below but I have no idea how to combine these things together.
ChoiceBox<BuySell> buySellBox = new ChoiceBox<>();
buySellBox.getItems().addAll("Buy", "Sell");
Update: Problem is still unresolved. However, by following the answer in this post, this is what I have got so far. After creating an object, a table row is created but when I click onto the table cell Buy
to edit, nothing happens (I was expecting a drop down choice box to appear and let me re-select my choice).
My table is editable, since I am able to edit tableCell using Volume
using the code above.
Added in the images below to show that I am able to edit the Volume
tableCell , but not the buySell
tableCell, whenever I click on it.
回答1:
Use a cell factory that creates a ChoiceBox
in the cell when editing. see this answer.
Example:
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
TableColumn<Item, String> col1 = new TableColumn("Column1");
col1.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
col1.setCellFactory(column -> new TableCel_Edit());
TableColumn<Item, Number> col2 = new TableColumn("Column2");
col2.setCellValueFactory(cellData -> cellData.getValue().valueProperty());
TableView<Item> table = new TableView();
table.setEditable(true);
table.getItems().addAll(loadData());
table.getColumns().addAll(col1, col2);
AnchorPane root = new AnchorPane();
root.getChildren().add(table);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String... args) {
Application.launch(args);
}
private List<Item> loadData() {
List<Item> list = new ArrayList();
for (int i = 0; i < 10; i++) {
Item item = new Item("item" + i, i);
list.add(item);
}
return list;
}
private class TableCel_Edit extends TableCell<Item, String> {
ChoiceBox<String> buySellBox = new ChoiceBox<>();
public TableCel_Edit() {
buySellBox.getItems().addAll("Buy", "Sell");
buySellBox.getSelectionModel().selectedIndexProperty().addListener((obs, oldValue, newValue) -> {
String value = buySellBox.getItems().get(newValue.intValue());
processEdit(value);
});
}
private void processEdit(String value) {
commitEdit(value);
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(getItem());
setGraphic(null);
}
@Override
public void commitEdit(String value) {
super.commitEdit(value);
// ((Item) this.getTableRow().getItem()).setName(value);
setGraphic(null);
}
@Override
public void startEdit() {
super.startEdit();
String value = getItem();
if (value != null) {
setGraphic(buySellBox);
setText(null);
}
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item);
}
}
}
}
Update: in your controller class add the following:
fxTransactionLogBuySell.setCellValueFactory(new PropertyValueFactory<Transaction,String>("buySell"));
fxTransactionLogBuySell.setCellFactory(column -> new TableCel_Edit());
You should also add this class (change Item
to Transaction
):
class TableCel_Edit extends TableCell<Item, String> {
....
}
来源:https://stackoverflow.com/questions/32026947/javafx-attach-choicebox-to-a-tablecell-on-a-tablecolumn-during-edit