繁体   English   中英

如何让 JTable 上的 MouseListener 工作

[英]How do I get the MouseListener on a JTable working

我正在尝试让JTableMouseListener一起使用。 现在的代码是带有图标的JTable的示例程序。 我想要的是,如果您双击一行,则应打开一个dialog ,其中包含整行的信息或该行的index-number 但我的问题是命令: table.addMouseListener(this); 不工作,也许是因为构造函数?

我尝试在 main 方法中使用new object ,然后创建 MousListener。

 public class TableIcon extends JPanel
 {
   public TableIcon()
   {
       Icon aboutIcon = new ImageIcon("Mypath");
       Icon addIcon = new ImageIcon("Mypath");
       Icon copyIcon = new ImageIcon("Mypath");

       String[] columnNames = {"Picture", "Description"};
       Object[][] data =
       {
           {aboutIcon, "Pic1"},
           {addIcon, "Pic2"},
           {copyIcon, "Pic3"},
       };

       DefaultTableModel model = new DefaultTableModel(data,columnNames)
       {
           public Class getColumnClass(int column)
           {
               return getValueAt(0, column).getClass();
           }

           public boolean isCellEditable(int row, int column)
           {
               return false;
           }

       };
       JTable table = new JTable( model );
       table.setPreferredScrollableViewportSize
           (table.getPreferredSize());
 // ################ MyError #########
       table.addMouseListener(this); // Error
 // ##################################
       JScrollPane scrollPane = new JScrollPane( table );
       add( scrollPane );  
   }

   private static void createAndShowGUI()
   {
       TableIcon test = new TableIcon();

       JFrame frame = new JFrame("Table Icon");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.add(test);
       frame.setLocationByPlatform( true );
       frame.pack();
       frame.setVisible( true );
   }

   public static void main(String[] args)
   {
       EventQueue.invokeLater(new Runnable()
       {
           public void run()
           {
               createAndShowGUI();
           }
       });
   }
   public void mouseClicked(MouseEvent e)
   {
       System.out.println("clicked");
   }
}

在这段代码中,我希望print带有“clicked”,但我得到的只是这个错误My Error TableIcon cannot be cast to java.awt.event.MouseListener

尝试使用适配器 class:

table.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 2) {
        //Code for handling the double click event
      }
    }
});

您指this是 class TableIcon并且它没有实现接口MouseListener ,但方法addMouseListener()需要它。

public void addMouseListener(MouseListener l)

如果您想 go 使用TableIcon class 的方法作为事件处理程序,然后添加MouseListener接口的实现。

public class TableIcon extends JPanel implements MouseListener {

 ...

 public void mouseClicked(MouseEvent e) {
    // code for handling of the click event
 }

 public void mouseEntered(MouseEvent e) {
 }

 public void mouseExited(MouseEvent e) {
 }

 public void mousePressed(MouseEvent e) {
 }

 public void mouseReleased(MouseEvent e) {
 }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM