0

I want the program to show context menu when user starts typing. The context menu should display all the String from array list which the user is typing (Like search). It should change as the user types or changes value in text field. The problem is its working but it is giving me duplicate values. but when i try printing the sorted list, it doesnt contain duplicate values. I have attach pictures below.

Screenshot 1 Screenshot 2

FIXED THE ISSUE

by removing the items from Context Menu with CM.getItems().clear();

Rather than removing it with loop. Somehow this was causing the issue CM.getItems().remove(i);

package application;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import com.jfoenix.controls.JFXAutoCompletePopup;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;

public class controller implements Initializable{
    @FXML TextField TF;
    @FXML ContextMenu CM = new ContextMenu();
    
    public void listener() {
        
        TF.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                
                
                for(int i=0;i<CM.getItems().size();i++) {
                    CM.getItems().remove(i);
                }
                TF.setContextMenu(CM);
                if (!newValue.isBlank()) {
                    CM.show(TF, Side.BOTTOM, 0, 0);
                    ArrayList<String> sortList = new ArrayList<String>();
                    for(int i=0;i<list.size();i++) {
                        String item = list.get(i);
                        //if(item.toLowerCase().regionMatches(true, 0, newValue.toLowerCase(), 0, newValue.length())) {
                        if(item.toLowerCase().startsWith(newValue.toLowerCase())) {
                            sortList.add(item);
                        } 
                    }
                    System.out.println(sortList);
                    for(int i=0;i<sortList.size();i++) {
                        //MenuItem menuItem = new MenuItem(sortList.get(i));
                        MenuItem menuItem = new MenuItem(sortList.get(i));
                        CM.getItems().add(menuItem);
                        System.out.println(sortList.get(i) + ", " + CM.getItems().get(i).toString());
                    }
                    CM.show(TF, Side.BOTTOM, 0, 0);
                } else if(newValue.isBlank()) {
                    CM.hide();
                }
            }
        });
    }

    ArrayList<String> list;
    
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        listener();
        
        list = new ArrayList<String>();
        list.add("aaa");
        list.add("bat");
        list.add("bating");
        list.add("det");
        list.add("elf");
        list.add("foo");
        list.add("git");
        list.add("horse");

        
        CM.setOnAction(e -> {
            TF.setText(((MenuItem)e.getTarget()).getText());
            TF.positionCaret(((MenuItem)e.getTarget()).getText().length());
        });
        
        
    }
}

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<AnchorPane prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller">
   <children>
      <Pane layoutX="-28.0" layoutY="43.0" prefHeight="1080.0" prefWidth="1920.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <TextField fx:id="TF" layoutX="14.0" layoutY="14.0" />
         </children>
      </Pane>
   </children>
</AnchorPane>


0 Answers0