Java Swing implements presentation data, and filtering sorting

Source: Internet
Author: User

 Public class extends DefaultTableModel {    public relationcostctrtable (vector<vector<string>> Tablevalues, vector<string> columnnames) {        super(Tablevalues, columnnames);    }        @Override    Publicboolean iscelleditable (intint  column) {                returnfalse;    }    }
 Public classJtableDemo4extendsJFrame {JtableDemoTbale2 DefaultTableModel; /*** Cancel Button*/    PrivateJButton cancelbtn; //Select row index    intSelectedrow;  Public Static voidMain (string[] args) {JtableDemo4 demo=NewJtableDemo4 (); Demo.setvisible (true); }     PublicJtableDemo4 () { This. Settitle ("Input invoice associated cost contract");  This. Setdefaultcloseoperation (Jframe.exit_on_close);  This. SetSize (700, 450); //jframe Form Center         This. Setlocationrelativeto (NULL); String src= "/image/logo.png";//Picture PathImage image =NULL; Try{image= Imageio.read ( This. GetClass (). GetResource (SRC)); } Catch(IOException e) {e.printstacktrace (); }        //Create a Picture object         This. Seticonimage (image);//Set icon        FinalJScrollPane ScrollPane =NewJScrollPane ();  This. Getcontentpane (). Add (ScrollPane, borderlayout.center); //set the name of a column in JTableVector<string> ColumnNames =NewVector<string>(); Columnnames.add (Organization); Columnnames.add ("Contract Code"); Columnnames.add ("Contract primary Key"); Vector<Vector<String>> tablevalues =NewVector<vector<string>>();  for(introw = 1; Row < 29; row++) {Vector<String> ROWV =NewVector<string>(); Rowv.add ("A" +row); Rowv.add ("B" +row); Rowv.add ("C" +row);        Tablevalues.add (ROWV); }        //Create jtable (table)        FinalJTable table =NewJTable (); //Create a tabular modelDefaultTableModel =NewJtableDemoTbale2 (tablevalues, columnnames); FinalTablerowsorter<jtabledemotbale2> Sorter =NewTablerowsorter<jtabledemotbale2>(DefaultTableModel);                Table.setrowsorter (sorter); //set up a tabular model of jtableTable.setmodel (DefaultTableModel); Table.gettableheader (). setreorderingallowed (false); /** Add JTable to JScrollPane,*/Scrollpane.setviewportview (table); //set the selection mode of the table---to Radio modeTable.setselectionmode (listselectionmodel.single_selection); FinalJPanel Panel =NewJPanel ();        Panel.setbackground (Color.gray);  This. Getcontentpane (). Add (Panel, Borderlayout.south); FinalJTextField FilterText =NewJTextField (20); JButton GL=NewJButton ("Filter"); JButton GLCBHT=NewJButton ("Associated Cost contract");        Panel.add (FilterText);        Panel.add (GL);        Panel.add (GLCBHT); /*** Click the JScrollPane scroll panel to uncheck the line*/Scrollpane.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {//Cancel the selected row, but the index value of the selected row is still in, not canceledtable.clearselection (); //Displays the index value of the selected row. System.out.println ("-----------------" +Selectedrow);        }        }); Gl.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {String text=Filtertext.gettext (); if(text.length () = = 0) {Sorter.setrowfilter (NULL); } Else{sorter.setrowfilter (rowfilter.regexfilter (text));        }            }        }); Glcbht.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {//Gets the---index value of the selected row in JTableSelectedrow =Table.getselectedrow (); //gets the value to the specified cell                if(Selectedrow < 0)                    return; inti =Table.convertrowindextomodel (Table.getselectedrow ()); Object OA= Defaulttablemodel.getvalueat (i, 0); Object ob= Defaulttablemodel.getvalueat (i, 1); Object OC= Defaulttablemodel.getvalueat (i, 2); introw = Selectedrow+1; System.out.println ("+ row +" line, "+" content: "+ OA +" "+ OB +" "+oc);    }        }); }}

Java Swing implements presentation data, and filtering sorting

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.