Jtable uses setmodel for Data Binding

Source: Internet
Author: User

The home page defines a public model class,CodeAs follows:

 

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package tablemodel;

Import java. SQL. resultset;
Import java. util. Collections list;
Import javax. Swing. Table. abstracttablemodel;
Import zhinengkaiguanjiankong. Comm. loggerutil;

/**
* Database and jtable Data Binding record data storage objects
* @ Author http://www.my400800.cn
*/
Public class tablemodel extends acttablemodel {

Private int column = 4;
Private string [] columnname = NULL;
Private collections list <string []> resultset = NULL;

Public tablemodel (){
}

/** Creates a new instance of tablemodel */
Public tablemodel (resultset RS, string [] dscolumnname, string [] columnname) throws exception {
If (dscolumnname. length! = Columnname. Length ){
Throw new exception ("the specified jtable column is inconsistent with the specified database column and data binding is not allowed ");
}
This. columnname = columnname;
Column = columnname. length;
Resultset = new pipeline list <string []> ();
Try {
While (Rs. Next ()){
String [] ROW = new string [column];
For (INT I = 0; I <column; I ++ ){
Row [I] = Rs. getstring (dscolumnname [I]);
}
Resultset. Add (ROW );
}
} Catch (exception e ){
Loggerutil. writeerrlog (E );
Throw E;
// System. Out. println ("Err ");
}
}

/**
* Retrieve the total number of records
* @ Return total number of records
*/
Public int getrowcount (){
Return resultset. Size ();
}

/**
* Retrieve the total number of Columns
* @ Return total number of Columns
*/
Public int getcolumncount (){
Return column;
}

/**
* Get the data of the specified column in the specified row
* @ Param rowindex row
* @ Param columnindex Column
* @ Return
*/
Public object getvalueat (INT rowindex, int columnindex ){
String [] ROW = resultset. Get (rowindex );
Return row [columnindex];
}

/**
* Get the specified column name
* @ Param I specifies the column
* @ Return specifies the column name to return
*/
Public String getcolumnname (int I ){
Return columnname [I];
}
}

 

 

Model Implementation class. In this column, set the jtable column and database Field names.

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package tablemodel;

Import java. SQL. resultset;

/**
*
* @ Author 173.114
*/
Public class zhinengkaiguanjiankongviewtablemodel extends tablemodel {

Public zhinengkaiguanjiankongviewtablemodel (resultset RS) throws exception {
/**
* Record the RS database Field names listed in jtable
*/
Super (RS, new string [] {"ID", "i64val", "timeval", "val14", "val22", "adv", "val24 ", "val13 "},
New String [] {"ID", "name", "time", "status", "field", "attribute", "Remarks", "Switch ID "});
}
}

 

Data Binding code

Tablemodel = new zhinengkaiguanjiankongviewtablemodel (database obtained resultset );
Jtbl_datalist.setmodel (tablemodel );

The above code binds the data to the jtable through the model.

 

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.