您的位置:程序门 -> java -> gui 设计



表格事件处理问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


表格事件处理问题
发表于:2008-01-13 18:53:59 楼主
客户资料填加资料,欲在该界面中输入详细客户资料
包括客户名称,联系方式等,以及客户购买的产品信息
如何处理用户输入产品信息之后生成产品对象?
客户资料已经可以处理,就是不知道如何实现表格处理
另外还想将机器型号,购买途径,安装人员设定为下拉菜单以供选择
菜单具体内容通过数据库获得,源代码见附件

class   machinetablemodel   extends   abstracttablemodel{
/**
  *  
  */
private   static   final   long   serialversionuid   =   1l;
object[][]   data=   new   string[5][6];
object[]   column=   new   string[]{"机器型号","机身号码","购买途径","购买时间","安装人员","安装时间"};
public   machinetablemodel(){
addtablemodellistener(new   machinetablemodellistener());
}
public   int   getrowcount(){
return   data.length;
}

public   int   getcolumncount(){
return   column.length;
}

public   string   getcolumnname(int   i){
return   column[i].tostring();
}

public   object   getvalueat(int   row,   int   col)   {
return   data[row][col];
}

public   void   setvalueat(object   object,   int   row,   int   col){
data[row][col]=   object;
firetabledatachanged();
}

public   boolean   iscelleditable(int   i,   int   j){
return   true;
}

class   machinetablemodellistener   implements   tablemodellistener{
public   void   tablechanged(tablemodelevent   e)   {
for(int   i=   0;   i <   data.length;   i++){
for(int   j=   0;   j <   data[0].length;   j++){
system.out.println(data[i][j]);
}
}
}
}
}


machine类源码如下:
public   class   machine   {
private   string   index;
private   string   name,   sn;
private   string   buy_from,   buy_date;
private   string   install,   in_date;
public   machine(){

}

public   machine(string   name){
this.name=   name;
}

public   machine(string   name,   string   sn){
this.name=   name;
this.sn=   sn;
}
                public   boolean   add(){
boolean   flag=   false;
string   sql=   "insert   into   machine(name,   sn,   buy_from,   buy_date,   install,   in_date)   values('"+   name+   "','"+   sn   +   "','"+   buy_from   +   "','"+   buy_date   +   "','"+   install+   "','"+   in_date+   "');";
dbconnect   dbconn=   new   dbconnect();
try{
        int   i=   dbconn.EXECuteupdate(sql);
        if(i!=   0){
        flag=   true;
        }
}catch(exception   ex){
ex.printstacktrace();
}finally{
dbconn.close();
}
return   flag;
}
}
该帖包含附件:
发表于:2008-01-13 18:58:091楼 得分:0

图形界面如图...
发表于:2008-01-13 20:03:012楼 得分:0
改写你的表格模型:
java code
class machinetablemodel extends abstracttablemodel{ /** * */ private static final long serialversionuid = 1l; [color=#ff0000]java.util.arraylist<machine> mlist=new java.util.arraylist<machine>(); object[] column= new string[]{"机器型号","机身号码","购买途径","购买时间","安装人员","安装时间"}; [/color] public machinetablemodel(){ addtablemodellistener(new machinetablemodellistener()); } public int getrowcount(){ [color=#ff0000]return mlist.size(); [/color] } public int getcolumncount(){ return column.legnth; } public string getcolumnname(int i){ return column[i]; } public object getvalueat(int row, int col) { [color=#ff0000]machine m=mlist.get(row); if(i==0) return m.getindex(); else if(i==1) return m.getname(); ...[/color] } public void setvalueat(object object, int row, int col){ [color=#ff0000]machine m=mlist.get(row); if(i==0) m.setindex(object); else if(i==1) m.setname(object); ...[/color] firetabledatachanged(); } public boolean iscelleditable(int i, int j){ return true; } class machinetablemodellistener implements tablemodellistener{ public void tablechanged(tablemodelevent e) { [color=#ff0000]for(machine m: mlist){ forint j= 0; j < column.length; j++){ if(i==0) system.out.print(m.getindex()+"\t"); else if(i==1) system.out.print(m.getname()+"\t"); ... } system.out.println();[/color]} } } }
发表于:2008-01-13 20:04:333楼 得分:0
那些html代码去掉,我原以为在代码中也可以加上的。
发表于:2008-01-14 13:11:424楼 得分:0
另外还想将机器型号,购买途径,安装人员设定为下拉菜单以供选择  

这个很简单,只需要设置这些列的编辑器为下拉框即可。

eg:

jcombobox   box   =   new   jcombobox();
list   list   =   查询数据库;
for(object   o:   list)   {
    box.additem(o);
}

tablecolumn   column   =   table.getcolumn("机器型号");
column.setcelleditor(new   defaultcelleditor(box));
...
发表于:2008-01-16 18:36:565楼 得分:0
非常感谢你们的帮助,将abstracttablemodel更改后
图形界面如图所示,没有出现空白表格等待用户输入
这个如何去改啊?
发表于:2008-01-17 20:58:276楼 得分:0
双击不就可以编辑了???
发表于:2008-01-18 22:25:007楼 得分:0
表格实现下拉菜单已经实现,谢谢
现在就是有关表格输入后产生对象的问题
正在...努力的实现呢


快速检索

最新资讯
热门点击