简体   繁体   中英

Why is the return value always 0 from getSelectedIndex from a DefaultListModel?

I have an AutoCompleteTextField working with a DefaultListModel.

        options = new DefaultListModel<>();
    labelACField = new AutoCompleteTextField(options){
        @Override
        protected boolean filter(String text) {

        }
    };

The return value from options.getSelectedIndex() always returns 0 from actionPerformed, even users choose other items in the AutoCompleteTextField.

labelACField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            if (filteredIdObjs!=null){
                selectedIdx = options.getSelectedIndex();

For example, a user clicks "VV Vanguard Large-Cap ETF" or IVOV....or IVVD, or CVV.. the return value from options.getSelectedIndex() always returns 0. How do I get the item index that the user clicks? 在此处输入图像描述

That's an interface designed for a List. AutoCompleteTextField uses the ListModel interface for ease of use. But it doesn't use selection since it's inherently a text field.

The output is the text within the text field. Not list selection since a user can type in any arbitrary value without selecting a value from the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM