[英]Add mouse listener to JLabel in ListCellRenderer
I have a ListCellRenderer that consist of two JLabels; 我有一个由两个JLabel组成的ListCellRenderer。 one for text and one for an exit image (a little cross).
一个用于文本,一个用于出口图像(有点十字)。
I want to add a mouselistener to that little cross, when clicked, the item is removed from the JList. 我想在那个小十字架上添加一个鼠标监听器,当单击该项目时,该项目将从JList中删除。
You can try adding MouseListener to your JList directly as follows, 您可以尝试将MouseListener直接添加到JList中,如下所示,
list.addMouseListener(new MouseAdapter(){
public void mouseReleased(final MouseEvent e) {
if (e.isPopupTrigger()) {
// Get the position of the click
final int x = e.getX();
final int y = e.getY();
// Verify that the click occured on the selected cell
final int index = list.getSelectedIndex();
}
}
});
Now depending on index above you can achieve what you want to do. 现在,根据上面的索引,您可以实现想要执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.