当前位置: 技术问答>java相关
请问JTable怎样响应鼠标事件
来源: 互联网 发布时间:2014-12-27
本文导语: 我想做到这样的效果,当鼠标进入一个jtable的某一个cell中,更改cursor,并且让这个cell可以响应鼠标的单击事件。请问怎样做?我现在不知道应该add 什么listener,也不知道调用那个函数来addlistener. | //这是...
我想做到这样的效果,当鼠标进入一个jtable的某一个cell中,更改cursor,并且让这个cell可以响应鼠标的单击事件。请问怎样做?我现在不知道应该add 什么listener,也不知道调用那个函数来addlistener.
|
//这是我想的办法,可是只能得到某个cell在表中的位置,当然也可使其响应其它事件.
//但是一次只能选取一个cell而不能是几个不在同一行或列的值.
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTable table = new JTable(10,10);
public Test() {
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(table),BorderLayout.CENTER);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int i = table.rowAtPoint(e.getPoint());
int j = table.columnAtPoint(e.getPoint());
System.out.println("");
}
});
}
public static void main(String args[]) {
Test test = new Test();
test.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
test.pack();
test.setVisible(true);
}
}
你也可以去:http://www.codeguru.com/java/articles/663.shtml看看.
那有对你的问题的完整解答.有一个自写的table类.我想是符合你的需要的.
//但是一次只能选取一个cell而不能是几个不在同一行或列的值.
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTable table = new JTable(10,10);
public Test() {
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(table),BorderLayout.CENTER);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int i = table.rowAtPoint(e.getPoint());
int j = table.columnAtPoint(e.getPoint());
System.out.println("");
}
});
}
public static void main(String args[]) {
Test test = new Test();
test.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
test.pack();
test.setVisible(true);
}
}
你也可以去:http://www.codeguru.com/java/articles/663.shtml看看.
那有对你的问题的完整解答.有一个自写的table类.我想是符合你的需要的.
|
(COPY)
You will have to subclass JTable and override the following three methods -
public Component prepareEditor(TableCellEditor editor,int row, int column)
Prepares the editor by querying the data model for the value and selection state of the cell at row, column.
public void editingCanceled(ChangeEvent e)
Invoked when editing is canceled. The editor object is discarded and the cell is rendered once again.
public void editingStopped(ChangeEvent e)
Invoked when editing is finished. The changes are saved and the editor is discarded.
In the prepareCellEditor first call the super.prepareCellEditor(...). On the returned component add the FocusListener.
Now whenever the editor component gets focus the focusGained(..) of the focus listener will be called. Ditto for focus loss.
In the editingCancelled(..) and editingStopped(...) remove the FocusListener.
You will have to subclass JTable and override the following three methods -
public Component prepareEditor(TableCellEditor editor,int row, int column)
Prepares the editor by querying the data model for the value and selection state of the cell at row, column.
public void editingCanceled(ChangeEvent e)
Invoked when editing is canceled. The editor object is discarded and the cell is rendered once again.
public void editingStopped(ChangeEvent e)
Invoked when editing is finished. The changes are saved and the editor is discarded.
In the prepareCellEditor first call the super.prepareCellEditor(...). On the returned component add the FocusListener.
Now whenever the editor component gets focus the focusGained(..) of the focus listener will be called. Ditto for focus loss.
In the editingCancelled(..) and editingStopped(...) remove the FocusListener.