表视图中的JavaFX可编辑ComboBox [英] JavaFX editable ComboBox in a table view
问题描述
我在表格视图中使用可编辑的ComboBox单元格。这是我的ComboBox单元类
公共类ComboBoxCell扩展了TableCell< ClassesProperty,String> {
private ComboBox< String>组合框;
public ComboBoxCell(){
}
@Override
public void startEdit(){
super.startEdit();
if(comboBox == null){
createComboBox();
}
setGraphic(comboBox);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Platform.runLater(new Runnable(){
@Override
public void run(){
comboBox.requestFocus() ;
comboBox.getEditor()。requestFocus();
comboBox.getEditor()。selectAll();
}
});
}
@Override
public void cancelEdit(){
super.cancelEdit();
setText(String.valueOf(getItem()));
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
public void updateItem(String item,boolean empty){
super.updateItem(item,empty);
if(empty){
setText(null);
setGraphic(null);
} else {
if(isEditing()){
if(comboBox!= null){
comboBox.setValue(getString());
}
setGraphic(comboBox);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(getString());
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
private void createComboBox(){
// ClassesController.getLevelChoice()是String
的可观察列表comboBox = new ComboBox<>(ClassesController.getLevelChoice());
comboBox.setEditable(true);
comboBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
comboBox.setOnKeyPressed(new EventHandler< KeyEvent>(){
@Override
public void handle(KeyEvent t){
if(t.getCode()== KeyCode.ENTER ){
commitEdit(comboBox.getSelectionModel()。getSelectedItem());
} else if(t.getCode()== KeyCode.ESCAPE){
cancelEdit();
}
}
});
}
private String getString(){
return getItem()== null? :getItem()。toString();
}
}
我需要在表格上点击三次才能解决问题单元格获取组合框的文本字段以编辑内容。有两种方法可以实现两次点击吗?我甚至使用了Platforn runlater,但是当我第一次尝试编辑单元格时,只需点击三次鼠标,但第二次只需点击两次。
在重写的cell.startEdit()方法中,添加带有文本字段的ListView,然后将此listview添加到setGraphic。这将在选择行并单击单元格后直接显示列表视图,列表视图将在tablecell中,我还没有找到一种方法将其作为弹出窗口
I am using a editable ComboBox cell in a table view. Here is my ComboBox cell class
public class ComboBoxCell extends TableCell<ClassesProperty, String> {
private ComboBox<String> comboBox;
public ComboBoxCell() {
}
@Override
public void startEdit() {
super.startEdit();
if (comboBox == null) {
createComboBox();
}
setGraphic(comboBox);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Platform.runLater(new Runnable() {
@Override
public void run() {
comboBox.requestFocus();
comboBox.getEditor().requestFocus();
comboBox.getEditor().selectAll();
}
});
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(String.valueOf(getItem()));
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (comboBox != null) {
comboBox.setValue(getString());
}
setGraphic(comboBox);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(getString());
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
private void createComboBox() {
// ClassesController.getLevelChoice() is the observable list of String
comboBox = new ComboBox<>(ClassesController.getLevelChoice());
comboBox.setEditable(true);
comboBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2);
comboBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(comboBox.getSelectionModel().getSelectedItem());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
The problem I need to press three clicks on the table cell to get the text field of the combo box to edit the contents. Is there a way to make it in two clicks? I even used Platforn runlater but when I try to edit a cell at first time it takes three mouse clicks but at the second time only two clicks.
In your overridden cell.startEdit() method, Add a ListView with a textfield, then add this listview to setGraphic. This will show the listview directly once the row is selected and the cell is clicked, the listview will be inside the tablecell, I am yet to find a way to make it as a popup
这篇关于表视图中的JavaFX可编辑ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!