簡體   English   中英

將鼠標偵聽器添加到ListCellRenderer中的JLabel

[英]Add mouse listener to JLabel in ListCellRenderer

我有一個由兩個JLabel組成的ListCellRenderer。 一個用於文本,一個用於出口圖像(有點十字)。

我想在那個小十字架上添加一個鼠標監聽器,當單擊該項目時,該項目將從JList中刪除。

您可以嘗試將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();
          }
    }
});

現在,根據上面的索引,您可以實現想要執行的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM