Search code examples
javalistviewjavafxautocompleterichtextfx

Autocompletion in codearea in javafx


How could I create a listview at the current caret position in codearea while typing as autocompletion in javafx? So far I locate the word that is currently being typed and I see if that word is contained in the array. This is the code I have so far. Thank you in advance!

String[] keyphrases = new String[]{"int main(){\n}", "cout", "cin"};

((CodeArea)tabPane.getSelectionModel().getSelectedItem().getContent()).textProperty().addListener(new ChangeListener<String>()
                        {
                            @Override
                            public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {
                                CodeArea sto = ((CodeArea)tabPane.getSelectionModel().getSelectedItem().getContent());
                                String curr = "";
                                String currFinal = "";
                                for (int i = sto.getAnchor(); i > 0; i--) {
                                    if (sto.getText().charAt(i) == '\n' || sto.getText().charAt(i) == ' ') {
                                        break;
                                    }else {
                                        curr += sto.getText().charAt(i);
                                    }
                                }
                                for (int i = curr.length()-1; i >= 0; i--) {
                                    currFinal += curr.charAt(i);
                                }
                                System.out.println(currFinal);
                                ArrayList<String> fil = new ArrayList<String>();
                                for (int i = 0; i < keyphrases.length; i++) {
                                    if (keyphrases[i].contains(currFinal)) {
                                        fil.add(keyphrases[i]);
                                    }
                                }
                                //display fil as listview in caret position?
                            } 
                        });

Solution

  • Using Sai's answer I created my own solution!

    codeArea.textProperty().addListener(new ChangeListener<String>()
        {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {
                
                
                String curr = "";
                String currFinal = "";
                for (int i = codeArea.getAnchor(); i > 0; i--) {
                    if (codeArea.getText().charAt(i) == '\n' || codeArea.getText().charAt(i) == ' ') {
                        break;
                    }else {
                        curr += codeArea.getText().charAt(i);
                    }
                }
                
                for (int i = curr.length()-1; i >= 0; i--) {
                    currFinal += curr.charAt(i);
                }
                
                if (currFinal != "") {
                    ArrayList<String> fil = new ArrayList<String>();
                    for (int i = 0; i < keyphrases.length; i++) {
                        if (keyphrases[i].contains(currFinal)) {
                            fil.add(keyphrases[i]);
                        }
                    }
                    System.out.println("Fil " + fil);
                    if (popup != null) {
                        popup.hide();
                    }
                    if (fil.size() > 0) {
                        
                        
                        
                        ListView lop = new ListView();
                        for (int i = 0; i < fil.size(); i++) {
                            lop.getItems().add(fil.get(i));
                        }
                        
                        popup = new Popup();
                     
                        lop.setMaxHeight(80);
                        popup.getContent().addAll(lop);
                        popup.show(codeArea, codeArea.getCaretBounds().get().getMaxX(), codeArea.getCaretBounds().get().getMaxY());
                        codeArea.requestFocus();
                        
                    }
                    codeArea.requestFocus();
                }else { 
                    if (popup != null) {
                        popup.hide();
                    }
                }
            }
                
                
        });