-1

I have a project I am working on. I am trying to make a dictionary. For that, I have a .csv file with about 55000 words.I am using the trie data structure which has a startsWith() method which checks whether there is a word in the .csv file which matches the given prefix. I had managed to get it to work to find all words that match the given prefix and display them. Now, I have to develop this into a JavaFX app.

So, I thought of using a ComboBox which has its editable attribute set to true so that I could type into it and then the handler associated with the textProperty() of its editor would display all the words starting with given prefix in the listview of the combobox.

Now, the problem I have is that whenever I click the arrow button of the combobox the application stops responding (I think it's because the list view tries to resize itself to fit the items which are 55000). So, what I want to know is how to disable the arrow button entirely. I have tried to set its background-color to transparent but even then it can still be clicked I want to make it so that it is disabled and transparent basically the combobox ends up looking like a text field.

If there are better, more efficient ways of implementing a dictionary I would appreciate it if you could guide me.

ak777
  • 1
  • 1
  • Why not use a `TextField`? – SedJ601 Dec 30 '20 at 16:53
  • filter the list in onShowing – kleopatra Dec 30 '20 at 16:53
  • @Sedrick how would that allow for the user to select a word from all those that match the prefix – ak777 Dec 30 '20 at 17:16
  • Sounds like you need a autocomplete `TextField`. https://stackoverflow.com/questions/36861056/javafx-textfield-auto-suggestions – SedJ601 Dec 30 '20 at 19:14
  • @Sedrick how would I show the user the words that match the prefix that is written in the textfield? – ak777 Dec 30 '20 at 19:47
  • @Sedrick I want the user to type into the combobox and when the typed prefix matches that of any words that exist in the dictionary a popup is displayed of the matching words like it is in AutoComplete TextField but the problem is I have been told not to use autocomplete textfield and do it in other ways. So that's why I wanted to disable and make transparent the arrow button so that user can't click it. – ak777 Dec 31 '20 at 04:29

1 Answers1

0

The ListView is a virtual control that only shows a certain number of cells at a time, it doesn't need to "resize itself to the number of items" in any way that would lock up your GUI.

Does this demo program do what you want?

public class Main extends Application {

    public static void main(String[] args) throws IOException, URISyntaxException {
        launch(args);
    }


    @Override
    public void start(Stage stage) {
        List<String> rawWords = Collections.emptyList();
        try {
            URI wordURI = new URI("https://www-cs-faculty.stanford.edu/~knuth/sgb-words.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(wordURI.toURL().openStream(), StandardCharsets.UTF_8));
            rawWords = reader.lines().collect(Collectors.toCollection(() -> new ArrayList<>(6000)));
        } catch (IOException | URISyntaxException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // make the list at least as big as in the question
        while(rawWords.size() < 55000) {
            ArrayList<String> nextWords = new ArrayList<>(rawWords.size() * 2);
            nextWords.addAll(rawWords);
            nextWords.addAll(rawWords);
            rawWords = nextWords;
        }

        Collections.sort(rawWords);
        ObservableList<String> wordList = FXCollections.observableArrayList(rawWords);
        FilteredList<String> filteredList = new FilteredList<>(wordList);
        ComboBox<String> combo = new ComboBox<>(filteredList);
        combo.setEditable(true);
        combo.getEditor().textProperty().addListener((obs, oldVal, newVal) -> {
            filteredList.setPredicate(s -> newVal == null || newVal.isEmpty() || s.startsWith(newVal));
        });
        VBox vbox = new VBox(8,new Label("Dictionary ComboBox"),
                combo,
                new Label("\n\n\n\nThis space intentionally left blank.\n\n\n\n"));
        vbox.setPadding(new Insets(8));
        Scene scene = new Scene(vbox, 400, 300);
        stage.setTitle("Demo - Filtered Combobox List");
        stage.setScene(scene);
        stage.show();
    }
}
swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • it does have the facility to filter words but I want to remove the arrow button because if the combobox has to store 55000 words and the arrow button is clicked the program stops responding so I was asking for a way to remove the button so as to get rid of that problem – ak777 Dec 30 '20 at 18:34
  • I cannot reproduce that issue. I modified the above program to ensure there are at least 55000 words in the list. There was a slight pause when clicking the arrow, but that was it. – swpalmer Dec 30 '20 at 21:29
  • It is also unclear what the point is of using a combobox if you don't want the users to be able to activate the choice list? – swpalmer Dec 30 '20 at 21:32
  • I want the user to type into the combobox and when the typed prefix matches that of any words that exist in the dictionary a popup is displayed of the matching words like it is in AutoComplete TextField but the problem is I have been told not to use autocomplete textfield and do it in other ways. So that's why I wanted to disable and make transparent the arrow button so that user can't click it. – ak777 Dec 31 '20 at 04:28
  • _I want_ which is exactly what this answer is doing - spelling out _your_ homework for you ;) forget about your idea of solving it, it's a dead end .. – kleopatra Dec 31 '20 at 05:07
  • @kleopatra well fyi this isn't _my_ homework and the only thing I was asking was how to disable the arrow button in a combobox. And said that any _additional_ guidance in how to make a dictionary would be appreciated but I never said it is necessary. – ak777 Dec 31 '20 at 14:53
  • _only thing I was asking was how to disable the arrow button in a combobox_ and we (at least I'm) trying to convince you that's not the solution to the problem you described _the problem I have is that whenever I click the arrow button of the combobox the application stops responding_ - with those symptons, there's something else wrong (as this answer proves), your question sounds like an x-y-problem and you insist on looking into the wrong direction .. your choice, obviously *sigh-and-off – kleopatra Dec 31 '20 at 17:06
  • You can call show() manually when your filtered list is not empty(), but disabling the arrow on a combobox is not the right approach to this. You should probably just use a regular TextField and implement the list popup yourself. – swpalmer Dec 31 '20 at 17:07