Question

When trying to select item from list by doubleclick it's selecting once for both List, but after selecting by single click!!!

    listScrollPanel.setViewportView(categoryList);
    subCategoryList.setModel(new javax.swing.AbstractListModel() {
        String[] strings = {"Sub-category 1", "Sub-category 2", "Sub-category 3", "Sub-category 4", "Sub-category 5", "Sub-category 6"};
    });
subCategoryList.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        infoBox(Integer.toString(evt.getClickCount()), subCategory);
        if (evt.getClickCount() == 2) {
            subCategoryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
                @Override
                public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                    subCategoryListValueChanged(evt);
                }
            });
        }
    }
});
private void subCategoryListValueChanged(javax.swing.event.ListSelectionEvent evt) {

    subCategory = (String) subCategoryList.getSelectedValue();
    labelSC.setText(CAT_HEADER);
    listScrollPanel.setViewportView(categoryList);
}
private static void infoBox(String Message, String location) {
    JOptionPane.showMessageDialog(null, Message, location, JOptionPane.INFORMATION_MESSAGE);
}

How to let him listen only double-click? I have categoryList which after double-click and ListSelection changing viewportView to subCategory which works same! but they after first 2 double-click react on single-click!

Was it helpful?

Solution

You're adding a new ListSelectionListener every time a double mouse click event occurs. Each one of these will respond to single mouse clicks. You can just use a MouseListener on its own:

subCategoryList.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent evt) {
        if (evt.getClickCount() == 2) {
           String selectedValue = subCategoryList.getSelectedValue();
           // do stuff with selectedValue...
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top