Package test. Table;
Import java. AWT. dimension;
Import java. AWT. event. mouseadapter;
Import java. AWT. event. mouseevent;
Import java. util. vector;
Import javax. Swing. jbutton;
Import javax. Swing. jframe;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Import javax. Swing. Table. abstracttablemodel;
Import javax. Swing. Table. defaulttablemodel;
Public class jtabletest extends jframe {
Class tabletablemodel extends defaulttablemodel {
/**
*
*/
Private Static final long serialversionuid = 679265889547674796l;
Public final string [] column_names = new string [] {"column 0", "column 1", "column 2", "column 3 "};
Public tabletablemodel ()
{
}
Public int getcolumncount (){
Return column_names.length;
}
Public String getcolumnname (INT columnindex ){
Return column_names [columnindex];
}
// Set the table to read-only
Public Boolean iscelleditable (INT row,
Int column)
{
Return false;
}
}
Private jtable table;
Private int I = 0;
Private Int J = 0;
Private int rowi =-1;
/**
* Launch the application
* @ Param ARGs
*/
Public static void main (string ARGs []) {
Try {
Jtabletest frame = new jtabletest ();
Frame. setvisible (true );
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Create the frame
*/
Public jtabletest (){
Super ();
Settitle ("jtable test ");
Getcontentpane (). setlayout (null );
Setbounds (100,100,500,375 );
Setdefaclocloseoperation (jframe. exit_on_close );
Final jscrollpane scrollpane = new jscrollpane ();
Scrollpane. setbounds (10, 28,460,271 );
Getcontentpane (). Add (scrollpane );
Table = new jtable ();
Table. setshowgrid (true );
Table. addmouselistener (New usermouseadapter (){
/***//**
* Click an event
* @ Param e event Source Parameter
*/
Public void mousesingleclicked (mouseevent e ){
// System. Out. println ("single clicked! ");
Rowi = table. rowatpoint (E. getpoint (); // obtain the row number of the table.
If (rowi>-1)
System. Out. println ("click the mouse" + (tabletablemodel) Table. GetModel (). getvalueat (rowi, 0 ));
}
/***//**
* Double-click event
* @ Param e event Source Parameter
*/
Public void mousedoubleclicked (mouseevent e ){
// System. Out. println ("doublc clicked! ");
Rowi = table. rowatpoint (E. getpoint (); // obtain the row number of the table.
If (rowi>-1)
System. Out. println ("double-click mouse" + (tabletablemodel) Table. GetModel (). getvalueat (rowi, 0 ));
}
});
Table. setmodel (New tabletablemodel ());
Scrollpane. setviewportview (table );
Final jbutton button = new jbutton ();
Button. addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent arg0 ){
(Tabletablemodel) table. getModel ()). addrow (New String [] {"row" + (I ++), "contains one", "Row 2", "Row 3 "});
}
});
Button. settext ("add a row ");
Button. setbounds (10,308, 99, 23 );
Getcontentpane (). Add (button );
Final jbutton button_1 = new jbutton ();
Button_1.addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent arg0 ){
(Tabletablemodel) Table. GetModel (). insertrow (rowi> = 0? Rowi: 0), new string [] {"insert row" + (J ++), "insert Row 1", "insert Row 2 ", "insert Row 3 "});
}
});
Button_1.settext ("insert row ");
Button_1.setbounds (121,308, 99, 23 );
Getcontentpane (). Add (button_1 );
}
}
// Usermouseadapter. Java
Package test. Table;
Import java. AWT. event .*;
/**
* Double-click an event with a timer delay. Clicking and double-click events do not affect each other!
*/
Public class usermouseadapter
Extends java. AWT. event. mouseadapter {
Private Boolean flag = false; // used to determine whether a double-click event has been executed
Private int clicknum = 0; // determines whether to execute the double-click event.
Public void mouseclicked (mouseevent e ){
Final mouseevent me = E; // event Source
This. Flag = false; // each time you click the mouse to initialize double-click, the event execution flag is false.
If (this. clicknum = 1) {// when clicknum = 1, double-click the event.
This. mousedoubleclicked (me); // execute the double-click event
This. clicknum = 0; // initialization double-click event execution flag is 0
This. Flag = true; // double-click the event that has been executed. The event flag is true.
Return;
}
// Define the timer
Java. util. Timer timer = new java. util. Timer ();
// The timer starts to execute. after a delay of 0.2 seconds, determine whether to execute the click event.
Timer. Schedule (New java. util. timertask (){
Private int n = 0; // records the number of timer executions
Public void run (){
If (FLAG) {// If the double-click event has been executed, cancel the execution directly.
N = 0;
Clicknum = 0;
This. Cancel ();
Return;
}
If (n = 1) {// After the timer waits for 0.2 seconds, the double-click event still does not occur. Execute the Click Event
Mousesingleclicked (me); // execute the Click Event
Flag = true;
Clicknum = 0;
N = 0;
This. Cancel ();
Return;
}
Clicknum ++;
N ++;
}
}, New java. util. Date (), 200); // sets the delay time.
}
/***//**
* Click an event
* @ Param e event Source Parameter
*/
Public void mousesingleclicked (mouseevent e ){
// System. Out. println ("single clicked! ");
}
/***//**
* Double-click event
* @ Param e event Source Parameter
*/
Public void mousedoubleclicked (mouseevent e ){
// System. Out. println ("doublc clicked! ");
}
}