[英]How do I get the MouseListener on a JTable working
I'm trying to get a JTable
working with a MouseListener
.我正在尝试让
JTable
与MouseListener
一起使用。 The code right now is a sample program for a JTable
with Icons.现在的代码是带有图标的
JTable
的示例程序。 What I want is, that if you double click on a row, a dialog
should open with informations from the whole row or just the index-number
from the row.我想要的是,如果您双击一行,则应打开一个
dialog
,其中包含整行的信息或该行的index-number
。 But my problem is that the command: table.addMouseListener(this);
但我的问题是命令:
table.addMouseListener(this);
is not working, maybe is it because of the constructor?不工作,也许是因为构造函数?
I tried to use a new object
inside the main method and create then the MousListener.我尝试在 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");
}
}
In this code I expect a print
with"clicked" but all I get is this error My Error TableIcon cannot be cast to java.awt.event.MouseListener在这段代码中,我希望
print
带有“clicked”,但我得到的只是这个错误My Error TableIcon cannot be cast to java.awt.event.MouseListener
Try using Adapter class:尝试使用适配器 class:
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
//Code for handling the double click event
}
}
});
You refer to this
which is class TableIcon
and it does not implement interface MouseListener
, but method addMouseListener()
expects it.您指
this
是 class TableIcon
并且它没有实现接口MouseListener
,但方法addMouseListener()
需要它。
public void addMouseListener(MouseListener l)
If you want to go with approach of TableIcon
class acting as event handler as well, then add implementation for the MouseListener
interface.如果您想 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.