javafx:在编辑期间将ChoiceBox附加到TableColumn上的TableCell [英] javafx: attach ChoiceBox to a TableCell on a TableColumn during Edit
问题描述
下面的代码是对象的实例方法。
private StringProperty buySell;
// getters
public String getBuySell(){
return this.buySell.get();
}
//返回属性对象
public StringProperty buySellProperty(){
return this.buySell;
}
// setters
public void setBuySell(String buySell){
this.buySell.set(buySell);
}
在我的控制器
class,我已经为 buySell
字符串属性创建了一个 TableColumn
,其代码如下。
当我创建一个事务时, tableView
上会有一个新行。但是,我希望能够编辑buySell tableCell 。
问题:如何嵌入 choicebox
,其值为在
, setOnEdit
函数中购买卖
,这样当我双击单元格时,它会给我一个选择框?
我的 choicebox
代码如下,但我不知道如何结合这些东西在一起。
ChoiceBox< BuySell> buySellBox = new ChoiceBox<>();
buySellBox.getItems()。addAll(买,卖);
更新:问题仍未解决。但是,按照
编辑时,使用在单元格中创建 ChoiceBox
的单元工厂。请参阅此回答。
示例:
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);
场景场景=新场景(根);
stage.setScene(场景);
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);
}
返回清单;
}
私有类TableCel_Edit扩展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);
}
}
}
}
控制器类中的
更新:
添加以下内容:
fxTransactionLogBuySell.setCellValueFactory(new PropertyValueFactory< Transaction,String>(buySell));
fxTransactionLogBuySell.setCellFactory(column - > new TableCel_Edit());
您还应该添加此类(更改项目
到交易
):
class TableCel_Edit扩展TableCell< Item,String> ; {
....
}
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.
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> {
....
}
这篇关于javafx:在编辑期间将ChoiceBox附加到TableColumn上的TableCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!