java.awt.RobotThis class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. Check the JDK document to find out how to do .Good luck!
同意
你觉得触发mouseclick事件可行吗?
怎么来触发呢,这可没有触发键方便哦!
请继续讨论?
老外可有讨论?
import javax.swing.*;import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;public class TableTest extends JFrame
{
JTable jTable1 = new JTable(3,4); public TableTest()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void main(String[] args)
{
TableTest tableTest1 = new TableTest();
tableTest1.show();
} protected void processWindowEvent(WindowEvent e){
if(e.getID()==e.WINDOW_CLOSING){
System.exit(0);
}
super.processWindowEvent(e);
}
private void jbInit() throws Exception
{
this.setSize(300,400);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
ListSelectionListener listener = new MySelectionListner();
jTable1.getSelectionModel().addListSelectionListener(listener);
jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);
} class MySelectionListner implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting())
return ;
if(e.getLastIndex()==e.getFirstIndex())
return;
int row = jTable1.getSelectedRow();
int col = jTable1.getSelectedColumn();
System.out.println("selected row is :"+row+" selected column " + col);
if(row>=0&&col>=0)
{
jTable1.editCellAt(row,col);
System.out.println("aaaaaa");
}
}
}
}抱歉,来完了,给我分:)
请漂流兄先到下面的帖子随便回复一下,我好给分。
谢谢!!!http://www.csdn.net/expert/topic/128/128006.shtm
import javax.swing.*;import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
public class TableTest extends JFrame
{
JTable jTable1 = new JTable(3,4); public TableTest()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void main(String[] args)
{
TableTest tableTest1 = new TableTest();
tableTest1.show();
} protected void processWindowEvent(WindowEvent e){
if(e.getID()==e.WINDOW_CLOSING){
System.exit(0);
}
super.processWindowEvent(e);
}
private void jbInit() throws Exception
{
this.setSize(300,400);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
ListSelectionListener listener = new MySelectionListner();
jTable1.getSelectionModel().addListSelectionListener(listener);
jTable1.getColumnModel().getSelectionModel().addListSelectionListener(listener);
} class MySelectionListner implements ListSelectionListener{
int lastRow,lastCol;
public void valueChanged(ListSelectionEvent e){
/* if(e.getValueIsAdjusting())
{
System.out.println("value is adjusting!!");
return ;
} */
int row = jTable1.getSelectedRow();
int col = jTable1.getSelectedColumn();
if(lastRow==row && lastCol==col)
{
System.out.println("The same Cell");
return;
} jTable1.getCellEditor(lastRow,lastCol).cancelCellEditing();
System.out.println("selected row is :"+row+" selected column " + col);
if(row>=0&&col>=0)
{
jTable1.editCellAt(row,col);
TableCellEditor cellEditor = jTable1.getCellEditor(row,col);
Component component = cellEditor.getTableCellEditorComponent
(jTable1,jTable1.getValueAt(row,col),true,row,col);
component.requestFocus();
}
}
}
}这样可以在同一列中获得光标,可是不能用键盘换到不同列,我现在没时间,下午再看看问题出在哪儿
为了检查是不是没有单击就不出现textedit,clickCountToStart设为2,看看双击会不会出现这样的问题.