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.stack.imgur.com/Bex5U.png
And when double clicked,a new frame should appear (in code "InstantMessageDialog" object)
https://i.stack.imgur.com/yjvW6.png
And it should look like this.