简体   繁体   中英

How to modify the value of JTextField after automatic item selection from popup menu by mouse click of editable JCombobox

I have an editable JCombobox . I have already done whats required for loading data from database.

After loading the data, I add some extra data like .next. or - for a specific reason. .next. or - which only work when they are highlighted in the popup menu.I have designed their working already.

However I don't want to view/selected .next. or - in JTextField of JComboBox.

For this purpose, I override JCombobox,

searchCBX.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED
                    && (".next.".equals(e.getItem()) || "-".equals(e.getItem()))) {
                searchTF.setText("");
            }
        }

    });

Here, searchCBX is my required combobox and searchTF is my textfield of searchCBX. It works fine when I try to select .next. or - by scrolling the JPopupmenu from keyboard, searchTF automatically goes to empty.

Now the problem arises when I try to select .next. or - by mouse click from visible popup menu. It gets selected automatically.

I am trying to override mouseListener but it's not working.

searchCBX.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            if ((".next.".equals(searchTF.getText()) || "-".equals(searchTF.getText()))) {
                searchTF.setText("");

            }
        }
    });

So how can I remove the selected data from searchTF after mouse click of a jComboBox popup menu where value is .next. or - . Any help would be really appreciated.

To my understand you need to remove the text of the searchTF once you select the .next. or - from the dropdown of the searchCBX . If its the case , you dont need to worry about a MouseListener here. Just the ItemStageChage event can do the work.

Here is a required part of the code :

public class Example extends JFrame {

private JComboBox searchCBX;
private JTextField searchTF;

/**
 * Creates new form Example
 */
public Example() {

    initComponents();

}

private void initComponents() {
    searchCBX = new JComboBox();
    searchTF = new JTextField();

    searchCBX.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent evt) {
            searchCBXItemStateChanged(evt);
        }
    });
}

private void searchCBXItemStateChanged(ItemEvent evt) {
    if (evt.getStateChange() == ItemEvent.SELECTED
            && (".next.".equals(evt.getItem()) || "-".equals(evt.getItem()))) {
        searchTF.setText("");
    } else {
        searchTF.setText(searchCBX.getSelectedItem().toString());
    }
}
}

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