Java中基于自定義表格模型表格實(shí)現(xiàn)方法探究_第1頁
Java中基于自定義表格模型表格實(shí)現(xiàn)方法探究_第2頁
Java中基于自定義表格模型表格實(shí)現(xiàn)方法探究_第3頁
Java中基于自定義表格模型表格實(shí)現(xiàn)方法探究_第4頁
Java中基于自定義表格模型表格實(shí)現(xiàn)方法探究_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、java中基于自定義表格模型表格實(shí)現(xiàn)方法探究摘要:jtable是java swing開發(fā)工具包中的表格組件。 該文介紹了通過定義一個(gè)自定義表格模型來構(gòu)造和使用表 格的方法。關(guān)鍵詞:java;表格;表格模型中圖分類號(hào):tp311文獻(xiàn)標(biāo)識(shí)碼:a文章編號(hào): 1009-3044(2012)15-3583-03research on implementation method of table based on custom table model in javapan guo-rong(chang zhou liu guo-jun higher vocational and technical sch

2、ool, changzhou 213025, china)abstract: jtable is the table componets of java swing development kit this paper discusses the method of construeting and using tables by defining a custom model.key words: java; table; table model因java程序設(shè)計(jì)語言具有強(qiáng)大的功能,正越來越多地 被軟件開發(fā)人員所使用。使用java既可以編寫基于windows 的圖形用戶界面gui程序,也可以

3、編寫在internet網(wǎng)絡(luò)環(huán) 境下運(yùn)行的web應(yīng)用程序?,F(xiàn)在編寫gui程序主要使用swing 開發(fā)工具包,它以抽象窗口工具包(awt)為基礎(chǔ)使跨平臺(tái) 應(yīng)用程序可以使用任何可插拔的外觀風(fēng)格。如果要在程序界 面中顯示大量數(shù)據(jù),并且經(jīng)常和數(shù)據(jù)庫中數(shù)據(jù)進(jìn)行交互,可 以使用swing開發(fā)工具包中的表格jtable組件,該組件是 較為復(fù)雜的組件之一。jtable組件控制數(shù)據(jù)的顯示方式,但jtable組件中存 儲(chǔ)的數(shù)據(jù)是由表格模型決定的,因此在我們創(chuàng)建jtable組 件前,應(yīng)先創(chuàng)建一個(gè)表格模型。swing開發(fā)工具包中提供了 好幾種 表格模 型類, 如 defaulttablemodel、 abstractt

4、ablemodel,但直接使用它們來構(gòu)造表格還顯得不 太方便和靈活,通??梢宰约憾x一個(gè)模型類來構(gòu)造表格。1自定義表格模型的定義可以將swing中提供的抽象表格模型類 abstracttablemodel作為基類,派生出一個(gè)表格模型類,在 該類中,重新定義了構(gòu)造方法以及向表格中添加數(shù)據(jù)行的方 法、刪除表格中數(shù)據(jù)行的方法、設(shè)置和讀取表格單元格中數(shù) 據(jù)的方法及獲取表格中行數(shù)的方法等,通過設(shè)計(jì)這些方法, 大大方便了對(duì)表格中數(shù)據(jù)的操縱。自定義表格模型類mytablemodel. java的定義如下: import java .util.*;import java. sql. *;import java

5、x swing table *;import javax swing. *;public classmytablemodelextendsabstracttablemodel private vector contentful 1; /存放表格數(shù)據(jù)的 vectorprivate stringe title_name; /存放表格標(biāo)題的數(shù) 組private int colcnt; /表格中的列數(shù)private int colst, colend; /可以編輯列的起止索引 號(hào)/構(gòu)造方法public mytablemodel (stringe title_name,int n)this. title

6、_ndme二new stringen;colcnt=n;for (int i=0;irow) content. remove(row);/獲取行數(shù)public int getrowcount()return content.size();/獲取某一單元格的值public object getvalueat(int row,int col)return (vector)content get(row). get(col) ;/更新表格中某一單元格的值public void setvalueat(object value,int row,int(vector)content, get (row).

7、 remove(col);(vector)content. get(row). add(col,value); this firetablecellupdated&ow, col);/設(shè)定可編輯列的起止范圍,從colst到colend public void setcolumneditable (int colst,int colend)this.colst二colst;this. colend=colend;/決定表格中哪些單元格的值可以修改,返回false表 示不能修改public boolean iscelleditable(int rowindex,int columninde

8、x)/序列號(hào)不能修改 if(columnindex>=colst && columnlndex<=colend) return true;return false;/獲取列名public string getcolumnname(int col) return title_namecol;/獲取列數(shù)public int £etcolumncount() return title_name. length;2自定義表格模型的使用表格模型定義好后,要使用表格就比較方便了。下面通 過從數(shù)據(jù)庫學(xué)生情況表(學(xué)號(hào):ssno,姓名:ssname,性別: ssex,備注:

9、ss? note)中將數(shù)據(jù)取至表格中來說明其主要 步驟。2. 1定義表格標(biāo)題string數(shù)組、jtable組件對(duì)象、及 自定義表格模型mytablemodel的對(duì)象string: heads= “序號(hào)”,”學(xué)號(hào)”,”姓名",“性 別”,”備注” ; jtable table ;mytablemodel model;2. 2生成jtable組件對(duì)象、自定義表格模型 mytablemodel 的對(duì)象model=new mytablemodel (heads,5);table=newjtable (model);p. add(new jscrollpane(table);2. 3從數(shù)據(jù)庫中

10、查詢數(shù)據(jù),將數(shù)據(jù)填充至表格中vector row;vector rows=new vector ();int i=l;try statement stat=scoremis .con. createstatement(); / scoremis 為主類resuitset resuit二stat. executequery ( “select * from sstudent where left (ssno, 4) = ' ” +cursc+”, order by ssno”);/ curse為代表當(dāng)前班級(jí)的成員變量while(result.next ()row二new vector (

11、);row. add(string. valueof(i+);row. add(resultgetstring( “ssno” );row. add(resuit. getstring( "ssname” );row. add(resuit. getstrin£( "ssex"); row. add(resuit. getstring( "ssnote");rows, add (row);model. addrows(rows);model, firetabledatachanged();catch(sqlexception e2) joptionpane. showmessagedialog(this,"操作數(shù)據(jù) 庫出錯(cuò):"+e2);catch(exception e3) joptionpane. showmessagedialog(this,"出現(xiàn)其他 錯(cuò)誤:” +e3);3結(jié)束語通過上述例程代碼可以看出,在使用了自定義表格

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論