Import java. AWT. borderlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. util. vector;
Import javax. Swing. jbutton;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Public class jtableoperation extends jframe {
Public jtableoperation ()
{
Super ();
Settitle ("table ");
Setbounds (100,100,500,500 );
Setdefaclocloseoperation (jframe. exit_on_close );
Jscrollpane scrollpane = new jscrollpane (); // supports scrolling.
Getcontentpane (). Add (scrollpane, borderlayout. center );
String [] columnnames = {"A", "B", "C", "D", "E", "F", "G "};
Vector columnnamev = new vector (); // get the header
For (INT column = 0; column <columnnames. length; column ++)
{
Columnnamev. Add (columnnames [column]);
}
Vector tablevaluev = new vector ();
For (int row = 1; row <21; row ++) // obtain data
{
Vector rowv = new vector ();
For (INT column = 0; column <columnnames. length; column ++)
{
Rowv. Add (columnnames [column] + row); // data
}
Tablevaluev. Add (rowv );
}
Final jtable table = new mytable (tablevaluev, columnnamev); // for a custom table, refer to the custom class on the previous page.
Table. setrowselectioninterval (1, 3); // you can specify
Table. addrowselectioninterval (5, 5); // append
Scrollpane. setviewportview (table );
Jpanel buttonpanel = new jpanel (); // button panel
Getcontentpane (). Add (buttonpanel, borderlayout. South); // below.
Jbutton selectallbutton = new jbutton ("select all ");
Selectallbutton. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent e ){
Table. selectall (); // select all rows
}
});
Buttonpanel. Add (selectallbutton );
Jbutton clearselectionbutton = new jbutton ("unselect ");
Clearselectionbutton. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent e ){
Table. clearselection (); // cancel the selection
}
});
Buttonpanel. Add (clearselectionbutton );
System. Out. println ("tables:" + Table. getrowcount () + "row" + Table. getcolumncount () + "column ");
System. Out. println ("Total:" + Table. getselectedrowcount () + "Row selected ");
System. Out. println ("Status of the third row:" + Table. isrowselected (2); // the third row
System. Out. println ("5th rows:" + Table. isrowselected (4); // 5th rows
System. out. println ("the index of the first row selected:" + table. getselectedrow (); // the index of the first row selected. If no index is selected,-1 is returned.
Int [] selectrows = table. getselectedrows (); // obtain all selected Indexes
System. Out. println ("The following are all selected rows :");
For (INT I = 0; I <selectrows. length; I ++ ){
System. Out. println (selectrows [I] + "");
}
System. Out. println ();
System. Out. println ("the name of the first 2nd columns before column movement is:" + Table. getcolumnname (1); // 2nd Columns
System. Out. println ("values of the first 2nd rows and columns in the column:" + Table. getvalueat (1, 1); // 2nd
Table. movecolumn (1, 5 );
System. Out. println ("Name of the 2nd column after the column is moved:" + Table. getcolumnname (1 ));
System. Out. println ("the value of Column 2nd after column movement:" + Table. getvalueat (1, 1 ));
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
// Jtableoperation
Jtableoperation = new jtableoperation ();
Jtableoperation. setvisible (true );
}
}