如何在TextArea上使用TAB / Enter KeyPressed,并使用focustraversal替换或输入密钥而不使用内部API?

时间:2016-11-21 23:17:48

标签: textarea javafx-8

我需要一个控件,它将自动换行,添加滚动条等 - 但忽略回车键并使用tab / shift选项卡跳转到下一个控件。我似乎无法做到这一点。

这是我所做的控制,它似乎只是停留在文本区域。 (这是从一个旧的在线示例中使用的,它似乎仅在textArea与其余节点位于同一节点时才有效。)

public class TabAndEnterIgnoringTextArea extends TextArea {

final TextArea myTextArea = this;

public TabAndEnterIgnoringTextArea() {
    this.setWrapText(true);
    addEventFilter(KeyEvent.KEY_PRESSED, new TabAndEnterHandler());
}

private class TabAndEnterHandler implements EventHandler<KeyEvent> {
    private KeyEvent recodedEvent;

    @Override
    public void handle(KeyEvent event) {
        if (recodedEvent != null) {
            recodedEvent = null;
            return;
        }


        Parent parent = getParent();
        if (parent != null) {
            switch (event.getCode()) {
            case ENTER:
                if (event.isControlDown()) {
                    recodedEvent = recodeWithoutControlDown(event);
                    myTextArea.fireEvent(recodedEvent);
                } else {
                    Event parentEvent = event.copyFor(parent, parent);
                    myTextArea.getParent().fireEvent(parentEvent);
                }
                event.consume();
                break;

            case TAB:
                if (event.isControlDown()) {
                    recodedEvent = recodeWithoutControlDown(event);
                    myTextArea.fireEvent(recodedEvent);
                } else if (event.isShiftDown()) {
                    ObservableList<Node> children = FXCollections.observableArrayList();
                    addAllDescendents(parent, children);
                    int idx = children.indexOf(myTextArea);
                    if (idx > 0) {
                        for (int i = idx - 1; i > 0; i--) {
                            if (children.get(i).isFocusTraversable()) {
                                children.get(i).requestFocus();
                                break;
                            }
                        }
                    }
                } else {
                    ObservableList<Node> children = FXCollections.observableArrayList();
                    addAllDescendents(parent, children);
                    int idx = children.indexOf(myTextArea);
                    if (idx >= 0) {
                        for (int i = idx + 1; i < children.size(); i++) {
                            if (children.get(i).isFocusTraversable()) {
                                children.get(i).requestFocus();
                                break;
                            }
                        }
                        if (idx + 1 >= children.size()) {
                            for (int i = 0; i < idx; i++) {
                                if (children.get(i).isFocusTraversable()) {
                                    children.get(i).requestFocus();
                                    break;
                                }
                            }
                        }
                    }
                }
                event.consume();
                break;
            default:
                break;
            }
        }
    }

    private void addAllDescendents(Parent parent, ObservableList<Node> nodes) {
        for (Node node : parent.getChildrenUnmodifiable()) {
            nodes.add(node);
            if (node instanceof Parent)
                addAllDescendents((Parent) node, nodes);
        }
    }

    private KeyEvent recodeWithoutControlDown(KeyEvent event) {
        return new KeyEvent(event.getEventType(), event.getCharacter(), event.getText(), event.getCode(),
                event.isShiftDown(), false, event.isAltDown(), event.isMetaDown());
    }
}

一旦我降落在我的田地里,就不会用键盘离开。有任何想法吗?另外 - 我不应该假设下一个控件实际上在我父节点内的节点中,因为控件可能是另一个控件的一部分,其中最后一个控件和下一个控件可能位于上面的父节点上。

基本上我想要场景图中的下一个可着陆项目。

我能够使用内部API - 但我知道这是非常气馁的。

public class TabAndEnterIgnoringTextArea extends TextArea {

final TextArea myTextArea = this;

public TabAndEnterIgnoringTextArea() {
    addEventFilter(KeyEvent.KEY_PRESSED, new TabAndEnterHandler());
}

class TabAndEnterHandler implements EventHandler<KeyEvent> {
    private KeyEvent recodedEvent;

    @Override
    public void handle(KeyEvent event) {
        if (recodedEvent != null) {
            recodedEvent = null;
            return;
        }

        Parent parent = myTextArea.getParent();
        Scene scene = parent.getScene();
        while (scene == null){
            parent = parent.getParent();
            scene = parent.getScene();
        }
        SceneTraversalEngine engine = new SceneTraversalEngine(getScene());

        if (parent != null) {
            switch (event.getCode()) {
            case ENTER:
                if (event.isControlDown()) {
                    recodedEvent = recodeWithoutControlDown(event);
                    myTextArea.fireEvent(recodedEvent);
                } else {
                    Event parentEvent = event.copyFor(parent, parent);
                    myTextArea.getParent().fireEvent(parentEvent);
                }
                event.consume();
                break;

            case TAB:
                if(event.isShiftDown()){
                    engine.trav(myTextArea, Direction.PREVIOUS);
                }else {
                    engine.trav(myTextArea, Direction.NEXT);
                }
            }
        }
    }

    private KeyEvent recodeWithoutControlDown(KeyEvent event) {
        return new KeyEvent(event.getEventType(), event.getCharacter(), event.getText(), event.getCode(),
                event.isShiftDown(), false, event.isAltDown(), event.isMetaDown());
    }
}

}

由于

1 个答案:

答案 0 :(得分:1)

我想我找到了一个解决方案,可以让我按照设计完成这项工作。

public class TabAndEnterIgnoringTextArea extends TextArea {

final TextArea myTextArea = this;

public TabAndEnterIgnoringTextArea() {
    this.setWrapText(true);
    addEventFilter(KeyEvent.KEY_PRESSED, new TabAndEnterHandler());
}

private class TabAndEnterHandler implements EventHandler<KeyEvent> {
    @Override
    public void handle(KeyEvent event) {
        if(event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
            event.consume();
            if(event.getCode() == KeyCode.TAB){
                selectNextNode(!event.isShiftDown());
            }
        }
    }

    private void selectNextNode(boolean forward){
        List<Node> nodes = getAllNodes(myTextArea.getScene().getRoot());
        int index = nodes.indexOf(myTextArea);
        if(forward){
            if(index < nodes.size() - 1) {
                nodes.get(index + 1).requestFocus();
            }else {
                nodes.get(0).requestFocus();
            }
        }else {
            if(index == 0) {
                nodes.get(nodes.size() - 1).requestFocus();
            }else {
                nodes.get(index - 1).requestFocus();
            }
        }
    }

    private ArrayList<Node> getAllNodes(Parent root) {
        ArrayList<Node> nodes = new ArrayList<Node>();
        addAllDescendents(root, nodes);
        return nodes;
    }

    private void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
        for (Node node : parent.getChildrenUnmodifiable()) {
            if(node.isFocusTraversable()){
                nodes.add(node);
            }
            if (node instanceof Parent)
                addAllDescendents((Parent)node, nodes);
        }
    }
}
}

如果您发现此方法有任何问题我会很感激,但它似乎适用于我的目的。