someJTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
//do something
}
}});
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
//do something
}
}});
解决方案 »
- java 线程的notify和wait不配套使用问题
- 请问如何判断一个面板里是否包含某个具体组件?
- 菜鸟问题,怎么返回今天日期前第十天的日期?
- 关于Blob 对象使用的问题,高手请进!!!!!
- 如何制作IE插件(分不够可以继续加)
- Arrays.binarySearch及String类的split方法问题
- 关于动态链接库的问题
- 求助诸位大侠 —— 关于文本框的问题!
- 這種編譯方法對嗎??
- 怎么做可以得到Java程序运行的帮助
- 我用JDBC连数据库时,总说我Class.forName(...)的Class有问题,为何呢
- 请问oicq的原理是什么,运行机制是什么?用java的socket能实现吗?需要了解那些基本协议?看那些书呢?
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.
TableModelEvent :当单元值变化,表格变化,单元更新时使用.
TableColumnModelEvent:列变化时.
ListSelectionEvent :行选取时变化.
ChangeEvent :编辑被停止/取消.JTable class 不提供任何添加监听器的方法.
监听器需直接添加到一个表格的三种模型或一个单元编辑器上.这是我抄的一个程序,希望对你有帮助.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.getModel().addTableModelListener(
new TableModelListener() {
public void tableChanged(TableModelEvent e) {
int firstRow = e.getFirstRow(),
column = e.getColumn(); String properties = " source=" + e.getSource() +
" firstRow= " +
(firstRow == TableModelEvent.HEADER_ROW ?
"HEADER_ROW" :
Integer.toString(firstRow)) + " lastRow= " + e.getLastRow() + " column= " +
(firstRow == TableModelEvent.ALL_COLUMNS ?
"ALL_COLUMNS" :
Integer.toString(column)); String typeString = new String();
int type = e.getType(); switch(type) {
case TableModelEvent.DELETE:
typeString = "DELETE"; break;
case TableModelEvent.INSERT:
typeString = "INSERT"; break;
case TableModelEvent.UPDATE:
typeString = "UPDATE"; break;
}
properties += " type=" + typeString; JOptionPane.showMessageDialog(Test.this,
e.getClass().getName() +
"[" + properties + "]");
}
});
}
public static void main(String args[]) {
new Test().show();
}
}
简单做法:JTable 中加入鼠标时件监听器,鼠标移动时看看是否在某个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("<"+i+","+j+">");
}
});
}
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类.我想是符合你的需要的.