An example of operations on jtable rows and columns

Source: Internet
Author: User

/**
Example of jtable row and column operations
Add rows and columns in the jtable, delete rows and columns, and select rows and columns...
*/
Package test;
Import java. util. arraylist;
Import java. AWT. event. actionlistener;
Import java. AWT. event. actionevent;
Import java. AWT. eventqueue;
Import javax. Swing. jtable;
Import javax. Swing. Table. defaulttablemodel;
Import javax. Swing. Table. tablecolumnmodel;
Import javax. Swing. Table. tablecolumn;
Import javax. Swing. jframe;
Import javax. Swing. jscrollpane;
Import javax. Swing. jmenu;
Import javax. Swing. jmenubar;
Import javax. Swing. jcheckboxmenuitem;
Import javax. Swing. jmenuitem;
Public class tableselectiontest {
Public static void main (string [] ARGs ){
Eventqueue. invokelater (New runnable (){
Public void run (){
Jframe frame = new tableselectionframe ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. setvisible (true );
}
});
}
}
Class tableselectionframe extends jframe {
Public tableselectionframe (){
Settitle ("tableselectiontest ");
Setsize (default_width, default_height );

// Set up multiplication table
Model = new defaulttablemodel (10, 10 );
For (INT I = 0; I <model. getrowcount (); I ++ ){
For (Int J = 0; j <model. getcolumncount (); j ++ ){
Model. setvalueat (I + 1) * (J + 1), I, j );
}
}
Table = new jtable (model );
Add (New jscrollpane (table), "center ");
Removedcolumns = new arraylist <tablecolumn> ();

Jmenubar menubar = new jmenubar ();
Setjmenubar (menubar );

Jmenu selectionmenu = new jmenu ("selection ");
Menubar. Add (selectionmenu );

Final jcheckboxmenuitem rowsitem = new jcheckboxmenuitem ("rows ");
Final jcheckboxmenuitem columnsitem = new jcheckboxmenuitem ("columns ");
Final jcheckboxmenuitem cellsitem = new jcheckboxmenuitem ("cells ");

Rowsitem. setselected (table. getrowselectionallowed ());
Columnsitem. setselected (table. getcolumnselectionallowed ());
Cellsitem. setselected (table. getcellselectionenabled ());

Rowsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Table. clearselection ();
Table. setrowselectionallowed (rowsitem. isselected ());
Cellsitem. setselected (table. getcellselectionenabled ());
}
});
Selectionmenu. Add (rowsitem );

Columnsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Table. clearselection ();
Table. setcolumnselectionallowed (columnsitem. isselected ());
Cellsitem. setselected (table. getcellselectionenabled ());
}
});
Selectionmenu. Add (columnsitem );

Cellsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Table. clearselection ();
Table. setcellselectionenabled (cellsitem. isselected ());
Rowsitem. setselected (table. getrowselectionallowed ());
Columnsitem. setselected (table. getcolumnselectionallowed ());
}
});
Selectionmenu. Add (cellsitem );

Jmenu tablemenu = new jmenu ("edit ");
Menubar. Add (tablemenu );

Jmenuitem hidecolumnsitem = new jmenuitem ("Hide columns ");
Hidecolumnsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Int [] selected = table. getselectedcolumns ();
Tablecolumnmodel columnmodel = table. getcolumnmodel ();
For (INT I = selected. length; I> = 0; I --){
Tablecolumn column = columnmodel. getcolumn (selected [I]);
Table. removecolumn (column );
Removedcolumns. Add (column );
}
}
});
Tablemenu. Add (hidecolumnsitem );

Jmenuitem showcolumnsitem = new jmenuitem ("show columns ");
Showcolumnsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
For (tablecolumn TC: removedcolumns ){
Table. AddColumn (TC );
}
Removedcolumns. Clear ();
}
});
Tablemenu. Add (showcolumnsitem );

Jmenuitem addrowitem = new jmenuitem ("add row ");
Addrowitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent e ){
Integer [] newcells = new integer [model. getcolumncount ()];
For (INT I = 0; I <newcells. length; I ++ ){
Newcells [I] = (I + 1) * (model. getrowcount () + 1 );
}
Model. addrow (newcells );
}
});
Tablemenu. Add (addrowitem );

Jmenuitem removerowsitem = new jmenuitem ("Remove rows ");
Removerowsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
Int [] selected = table. getselectedrows ();
For (INT I = selected. Length-1; I> = 0; I --){
Model. removerow (selected [I]);
}
}
});
Tablemenu. Add (removerowsitem );

Jmenuitem maid = new jmenuitem ("clear cells ");
Clearcellsitem. addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent event ){
For (INT I = 0; I <Table. getrowcount (); I ++ ){
For (Int J = 0; j <Table. getcolumncount (); j ++ ){
If (table. iscellselected (I, j )){
Table. setvalueat (0, I, j );
}
}
}
}
});
Tablemenu. Add (clearcellsitem );

}

Private defaulttablemodel model;
Private jtable table;
Private arraylist <tablecolumn> removedcolumns;

Private Static final int default_width = 400;
Private Static finals int default_height = 300;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.