Java table JTable Instance (with scroll bar, inline selection box)

Source: Internet
Author: User

Importjavax.swing.JTable;ImportJavax.swing.table.AbstractTableModel;ImportJavax.swing.JScrollPane;ImportJavax.swing.JFrame;ImportJavax.swing.JOptionPane;Importjava.awt.*;Importjava.awt.event.*; Public classJtabledemoextendsJFrame {Private BooleanDEBUG =true; PublicJtabledemo () {//Implementing a Construction methodSuper("Recorderofworkers");//first call the constructor of the parent class JFrame to generate a windowMytablemodel MyModel =NewMytablemodel ();//MyModel data for storing tablesJTable table =NewJTable (MyModel);//Table Object Table's data source is the MyModel objectTable.setpreferredscrollableviewportsize (NewDimension (500, 70));//display size of the table//produces a panel with a scroll barJScrollPane ScrollPane =NewJScrollPane (table);//Add a panel with scroll bars into the windowGetcontentpane (). Add (ScrollPane, borderlayout.center); Addwindowlistener (NewWindowadapter () {//Register Window Listener Public voidwindowclosing (windowevent e) {system.exit (0);}});}//storing the data to be displayed in a table in a string array and in an object arrayclassMytablemodelextendsAbstracttablemodel {//The contents of the first row in the table are stored in the string array columnnamesFinalString[] ColumnNames = {"First Name", "Position", "Telephone","Monthlypay", "Married" };//The contents of each row in the table are saved in the two-dimensional array dataFinalobject[][] data = {{ "Wangdong", "Executive", "01068790231",NewInteger (5000),NewBoolean (false) },{ "Lihong", "secretary", "01069785321",NewInteger (3500),NewBoolean (true) },{ "Lirui", "Manager", "01065498732",NewInteger (4500),NewBoolean (false) },{ "Zhaoxin", "Safeguard", "01062796879",NewInteger (2000),NewBoolean (true) },{ "Chenlei", "salesman", "01063541298",NewInteger (4000),NewBoolean (false) } };//The following method overrides the method in Abstracttablemodel, whose primary purpose is to be called by the JTable object so that it can be displayed correctly in the table. Programmers must be properly implemented based on the type of data being used. //get the number of columns Public intgetColumnCount () {returncolumnnames.length;}//get the number of rows Public intGetRowCount () {returndata.length;}//get the name of a column, and the name of the current column is saved in the string array columnnames PublicString getColumnName (intCol) {returnColumnnames[col];}//Gets the data for a column in a row, and the data is saved in the object array PublicObject getValueAt (intRowintCol) {returnData[row][col];}//determine the type of each cell PublicClass getColumnClass (intc) {returngetValueAt (0, C). GetClass ();}//declaring a table as editable Public BooleanisCellEditable (intRowintCol) {if(Col < 2) {return false;} Else {return true;}}//change the value of a data Public voidsetValueAt (Object value,intRowintCol) {if(DEBUG) {System.out.println ("Setting value at" + Row + "," +Col+ "to" + Value + "(an instance of" + value.getclass () + ")");}if(Data[0][col]instanceofInteger &&! (ValueinstanceofInteger)) {Try{Data[row][col]=NewInteger (value.tostring ()); firetablecellupdated (row, col);} Catch(NumberFormatException e) {joptionpane.showmessagedialog (Jtabledemo. This, "the \" "+getColumnName (COL)+ "\" column accepts only integer values. ");}} Else{Data[row][col]=value;firetablecellupdated (Row, col);}if(DEBUG) {System.out.println ("New Value of data:");p rintdebugdata ();}}Private voidPrintdebugdata () {intNumRows =GetRowCount ();intNumcols =getColumnCount (); for(inti = 0; i < numrows; i++) {System.out.print ("Row" + i + ":"); for(intj = 0; J < Numcols; J + +) {System.out.print (" " +data[i][j]);} System.out.println ();} System.out.println ("--------------------------");}} Public Static voidMain (string[] args) {Jtabledemo frame=NewJtabledemo (); Frame.pack (); Frame.setvisible (true);}}

Java table JTable Instance (with scroll bar, inline selection box)

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.