javaswingmouseeventjlist

Double click event on JList element


I've got a problem, and I can't find an answer by myself. I'm making a simple chat in java, and I populated a JList with names. When a name is double-clicked, that name needs to be passed as an argument to another frame (as a recipient name). But, double click does not work

I've got a main class InstantMessageFrame, in which a JList named friends is initialized and filled with an array of strings.

private JList<String> friends;
String names[] = { "Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna","Matea","Veronika","Vera","Marta","Mirta","Davor","Marko","Matko","Kloki" };
JList<String> friends = new JList<String>(names);

Also,I added a listener to my JList

DisplayMessageDialog dmd = new DisplayMessageDialog();
friends.addMouseListener(dmd);

This is my DisplayMessageDialog class which checks if there was a double click. If there is a double click, a new Frame should appear. None of the lines in the first "if" statement executes (one with the e.getClickCount())

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DisplayMessageDialog extends MouseAdapter
{
    public void mouseClicked(MouseEvent e)
    {
        JList theList = (JList) e.getSource();
        if (e.getClickCount() == 2) 
        {
            int index = theList.locationToIndex(e.getPoint());
            if (index >= 0) 
            {
                Object o = theList.getModel().getElementAt(index);
                InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
                imd.setVisible(true);
            System.out.println("Double-clicked on: " + o.toString());
            }
        }
    }
}

This is how it should look like:

https://i.sstatic.net/Bex5U.png

And when double clicked,a new frame should appear (in code "InstantMessageDialog" object)

https://i.sstatic.net/yjvW6.png

And it should look like this.


Solution

  • So working off your out-of-context code snippets, this example seems to work just fine, so I can only surmise that your problem is else where in your code that you're not showing us...

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JList<String> friends;
    
            public TestPane() {
                String names[] = {"Ana", "Banana", "Cikla", "Doris", "Ema", "Mirna", "Matea", "Veronika", "Vera", "Marta", "Mirta", "Davor", "Marko", "Matko", "Kloki"};
                JList<String> friends = new JList<String>(names);
    
                setLayout(new BorderLayout());
                add(new JScrollPane(friends));
    
                DisplayMessageDialog dmd = new DisplayMessageDialog();
                friends.addMouseListener(dmd);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.dispose();
            }
    
        }
    
        public class DisplayMessageDialog extends MouseAdapter {
    
            public void mouseClicked(MouseEvent e) {
                JList theList = (JList) e.getSource();
                //              if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
                if (e.getClickCount() == 2) {
                    int index = theList.locationToIndex(e.getPoint());
                    if (index >= 0) {
                        Object o = theList.getModel().getElementAt(index);
                                //InstantMessageDialog imd = new InstantMessageDialog(null, o.toString());
                        //imd.setVisible(true);
                        JOptionPane.showMessageDialog(theList, "Double-clicked on: " + o.toString());
                        System.out.println("Double-clicked on: " + o.toString());
                    }
                }
            }
        }
    }
    

    I might add though, you "double click" check probably should be something more like...

    if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
    

    Assuming your really only want to respond to left mouse clicks