Javafx 2 JavaFX:使用组合键进行自定义遍历

Javafx 2 JavaFX:使用组合键进行自定义遍历,javafx-2,Javafx 2,我有一个带有一个编辑组合框和三个可编辑文本字段的表, 可编辑字段的创建方式如下: public class EditingCell extends TableCell<Person, String> { private TextField textField; public EditingCell() { } @Override public void startEdit() { super.startEdit(); if (textField == null) {

我有一个带有一个编辑组合框和三个可编辑文本字段的表, 可编辑字段的创建方式如下:

public class EditingCell extends TableCell<Person, String> {
private TextField textField;
public EditingCell() {
}
@Override
public void startEdit() {
    super.startEdit();
    if (textField == null) {
        createTextField();
    }
    setGraphic(textField);
    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            textField.requestFocus();
            textField.selectAll();
        }
    });
}
@Override
public void cancelEdit() {
    super.cancelEdit();
    setText((String) getItem());
    setContentDisplay(ContentDisplay.TEXT_ONLY);
}
@Override
public void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setText(null);
        setGraphic(null);
    } else {
        if (isEditing()) {
            if (textField != null) {
                textField.setText(getString());
            }
            setGraphic(textField);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        } else {
            setText(getString());
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        }
    }
}
private void createTextField() {
    textField = new TextField(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                commitEdit(textField.getText());
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            } else if (t.getCode() == KeyCode.TAB) {
                commitEdit(textField.getText());
                TableColumn nextColumn = getNextColumn(!t.isShiftDown());
                if (nextColumn != null) {
                    getTableView().edit(getTableRow().getIndex(), nextColumn);
                }
            }
        }
    });
    textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if (!newValue && textField != null) {
                commitEdit(textField.getText());
            }
        }
    });
}
private String getString() {
    return getItem() == null ? "" : getItem().toString();
}
/**
 *
 * @param forward true gets the column to the right, false the column to the left of the current column
 * @return
 */
private TableColumn<Person, ?> getNextColumn(boolean forward) {
    List<TableColumn<Person, ?>> columns = new ArrayList<>();
    for (TableColumn<Person, ?> column : getTableView().getColumns()) {
        columns.addAll(getLeaves(column));
    }
    //There is no other column that supports editing.
    if (columns.size() < 2) {
        return null;
    }
    int currentIndex = columns.indexOf(getTableColumn());
    int nextIndex = currentIndex;
    if (forward) {
        nextIndex++;
        if (nextIndex > columns.size() - 1) {
            nextIndex = 0;
        }
    } else {
        nextIndex--;
        if (nextIndex < 0) {
            nextIndex = columns.size() - 1;
        }
    }
    return columns.get(nextIndex);
}

private List<TableColumn<Person, ?>> getLeaves(TableColumn<Person, ?> root) {
    List<TableColumn<Person, ?>> columns = new ArrayList<>();
    if (root.getColumns().isEmpty()) {
        //We only want the leaves that are editable.
        if (root.isEditable()) {
            columns.add(root);
        }
        return columns;
    } else {
        for (TableColumn<Person, ?> column : root.getColumns()) {
            columns.addAll(getLeaves(column));
        }
        return columns;
    }
}
public static void createEditingComboColumn(final TableColumn<DUMMY_PurchaseOrderLine, String> Column, final ObservableList<String>comboData, final ObservableList<DUMMY_PurchaseOrderLine> Pdata) {
 Column.setCellFactory(new Callback<TableColumn<DUMMY_PurchaseOrderLine,String>,TableCell<DUMMY_PurchaseOrderLine,String>>(){ 
 @Override
    public TableCell<DUMMY_PurchaseOrderLine, String> call(TableColumn<DUMMY_PurchaseOrderLine, String> p) {
    final TableCell<DUMMY_PurchaseOrderLine, String> cell = new TableCell<DUMMY_PurchaseOrderLine, String>(){
          @Override
           public void updateItem(String item, boolean empty) {
              if(item!=null){

             final  ComboBox editableComboBox = new ComboBox(comboData);
               editableComboBox.setEditable(true);
               editableComboBox.setMaxWidth(Double.MAX_VALUE);
               Platform.runLater(new Runnable() {
               @Override
                 public void run() {
                   editableComboBox.requestFocus();
                 }
               });
            setGraphic(editableComboBox);

          } 

         }
        };
    cell.addEventFilter(KeyEvent.KEY_PRESSED,new EventHandler<KeyEvent>(){
        @Override
        public void handle(KeyEvent t) {
            activeRow = cell.getIndex();
             if(t.getCode() == KeyCode.TAB||t.getCode() ==KeyCode.RIGHT){

                 cell.getTableView().edit(cell.getTableRow().getIndex(),cell.getTableView().getColumns().get(1) );

             } 
          }                
         }); 

         return cell;
        }
    }); 

}
公共类EditingCell扩展了TableCell{ 私有文本字段文本字段; 公共编辑单元(){ } @凌驾 公开作废已启动IT(){ super.startEdit(); if(textField==null){ createTextField(); } 设置图形(文本字段); setContentDisplay(仅限ContentDisplay.GRAPHIC_); Platform.runLater(新的Runnable(){ @凌驾 公开募捐{ textField.requestFocus(); textField.selectAll(); } }); } @凌驾 公共作废取消编辑(){ super.cancelEdit(); setText((字符串)getItem()); setContentDisplay(仅限ContentDisplay.TEXT_); } @凌驾 public void updateItem(字符串项,布尔值为空){ super.updateItem(项,空); if(空){ setText(空); 设置图形(空); }否则{ if(isEditing()){ if(textField!=null){ setText(getString()); } 设置图形(文本字段); setContentDisplay(仅限ContentDisplay.GRAPHIC_); }否则{ setText(getString()); setContentDisplay(仅限ContentDisplay.TEXT_); } } } 私有void createTextField(){ textField=newtextfield(getString()); textField.setMinWidth(this.getWidth()-this.getGraphicTextGap()*2); setOnKeyPressed(新的EventHandler(){ @凌驾 公共无效句柄(KeyEvent t){ if(t.getCode()==KeyCode.ENTER){ committedit(textField.getText()); }else if(t.getCode()==KeyCode.ESCAPE){ 取消编辑(); }else if(t.getCode()==KeyCode.TAB){ committedit(textField.getText()); TableColumn nextColumn=getNextColumn(!t.IsShift Down()); if(nextColumn!=null){ getTableView().edit(getTableRow().getIndex(),nextColumn); } } } }); textField.focusedProperty().addListener(新的ChangeListener()){ @凌驾
public void已更改(observeValue关键事件处理程序中的以下条件

if (t.getCode() == KeyCode.TAB)
将处理“TAB”+任何修饰符,这意味着它将处理“Ctrl+TAB”、“Alt+TAB”、“Shift+Alt+TAB”等。对于“Right”键也是如此。因此,要仅处理“TAB”,首选方法是定义
KeyCodeCombination

您发布的代码是部分的,不可执行,即。不是SSCCE。据我所知,您的问题在于键事件处理。因此,我编写了一个完全不同的代码,但演示了上述解释。请参阅示例,并尝试使用不同于“TAB”或“RIGHT”的键组合向前遍历。尝试使用“Ctrl+TAB”或者“Alt+RIGHT”。它不会像预期的那样对他们起作用

public class TraverseDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        final TextField textField1 = new TextField();
        final TextField textField2 = new TextField();
        final TextField textField3 = new TextField();

        // Diasble all traversals since we will manage them manually, for only textField2.
        textField1.setFocusTraversable(false);
        textField2.setFocusTraversable(false);
        textField3.setFocusTraversable(false);

        // Define key combinations for traversals.
        final KeyCombination kcTab = KeyCodeCombination.valueOf("TAB");
        final KeyCombination kcShiftTab = KeyCodeCombination.valueOf("Shift+TAB");
        final KeyCombination kcRight = KeyCodeCombination.valueOf("RIGHT");
        final KeyCombination kcLeft = KeyCodeCombination.valueOf("LEFT");

        // Default initial focused textfield
        requestFocus(textField2);

        textField2.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                if (kcTab.match(t) || kcRight.match(t)) {
                    System.out.println("traverse forward");
                    requestFocus(textField3);
                } else if (kcShiftTab.match(t) || kcLeft.match(t)) {
                    System.out.println("traverse backward");
                    requestFocus(textField1);
                }
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(HBoxBuilder.create().spacing(10).children(textField1, textField2, textField3).build());
        Scene scene = new Scene(root, 300, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void requestFocus(final Node node) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                node.requestFocus();
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public类TraverseDemo扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
final TextField textField1=新TextField();
final TextField textField2=新的TextField();
final TextField textField3=新TextField();
//由于我们将手动管理所有遍历,因此仅对textField2进行Diasble。
textField1.setFocusTraversable(false);
textField2.setFocusTraversable(false);
textField3.setFocusTraversable(false);
//定义遍历的组合键。
final KeyCombination kcTab=KeyCodeCombination.valueOf(“TAB”);
final KeyCombination kcShiftTab=KeyCodeCombination.valueOf(“Shift+TAB”);
final KeyCombination kcRight=keycodecompbination.valueOf(“RIGHT”);
final KeyCombination kcLeft=KeyCodeCombination.valueOf(“左”);
//默认初始聚焦文本字段
requestFocus(文本字段2);
textField2.addEventFilter(按下KeyEvent.KEY_,新建EventHandler()){
@凌驾
公共无效句柄(KeyEvent t){
if(kcTab.match(t)| | kcRight.match(t)){
System.out.println(“向前遍历”);
requestFocus(文本字段3);
}else if(kcShiftTab.match(t)| | kcleet.match(t)){
System.out.println(“向后遍历”);
请求焦点(textField1);
}
}
});
StackPane root=新的StackPane();
root.getChildren().add(HBoxBuilder.create().spating(10).children(textField1,textField2,textField3.build());
场景=新场景(根,300,150);
初级阶段。场景(场景);
primaryStage.show();
}
私有void requestFocus(最终节点){
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
requestFocus();
}
});
}
公共静态void main(字符串[]args){
发射(args);
}
}