ravengal
ravengal

Reputation: 13

How to do autopair with richtextfx?

I am working with javafx, richtextfx to be more specific. I have a codearea and I want it to add ")" when a user types "(" or add another quote when a user types a quote.

Currently I have tried this code, it has however not added anything to the codearea at all.

 public class MREX extends Application {
   private CodeArea mainCodeArea;

    @Override
    public void start(final Stage primaryStage) throws Exception {
        Scene scene = new Scene(createTextAreaBox());
        primaryStage.setScene(scene);

    }
    private Node createTextAreaBox() {
        CodeArea mainCodeArea = new CodeArea();
        mainCodeArea.addEventHandler(KeyEvent.KEY_TYPED, (KeyEvent KE) -> {
            switch (KE.getCode()){
                case QUOTE : {
                    mainCodeArea.insertText(mainCodeArea.getCaretPosition(), "k");
                    break;
                }
                case LEFT_PARENTHESIS : {
                    mainCodeArea.insertText(mainCodeArea.getCaretPosition(), ")");
                    break;
                }
            }
        });

        return mainCodeArea;
    }
}

Upvotes: 0

Views: 38

Answers (1)

ravengal
ravengal

Reputation: 13

Thanks to James_D I realized getCode was the wrong method to use. The code works just by changing the switch to be like this:

 switch (KE.getCharacter()){
            case "(" : {
                mainCodeArea.insertText(mainCodeArea.getCaretPosition(), ")");
                mainCodeArea.moveTo(mainCodeArea.getCaretPosition()-1);
                break;
            }

Upvotes: 1

Related Questions