简体   繁体   English

为什么 DefaultListModel 的 getSelectedIndex 的返回值总是 0?

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

I have an AutoCompleteTextField working with a DefaultListModel.我有一个使用 DefaultListModel 的 AutoCompleteTextField。

        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. options.getSelectedIndex() 的返回值总是从 actionPerformed 返回 0,即使用户在 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?例如,用户单击“VV Vanguard Large-Cap ETF”或 IVOV....或 IVVD,或 CVV.. options.getSelectedIndex() 的返回值始终返回 0。如何获取用户的项目索引点击? 在此处输入图像描述

That's an interface designed for a List.这是为列表设计的界面。 AutoCompleteTextField uses the ListModel interface for ease of use. AutoCompleteTextField使用ListModel接口以便于使用。 But it doesn't use selection since it's inherently a text field.但它不使用选择,因为它本质上是一个文本字段。

The output is the text within the text field. output 是文本字段中的文本。 Not list selection since a user can type in any arbitrary value without selecting a value from the list.不是列表选择,因为用户可以键入任意值而无需从列表中选择值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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