副标题#e#
列表在任何一门开拓语言中都占有很是重要的职位,在.Net中有GridView,在extjs中有GridPanel。。。,而在java Swing中,它的名字叫JTable。这两天在研究JTable的利用,也有一些收获,所以在这里跟各人分享交换一下,下面的内容将包罗:1)JTable的根基用法;2)奈何为JTable添加行点击响应事件,双击后打开窗口;3)奈何为JTable的行添加标识,如行id等;4)奈何在JTable中动态添加新行;
1)JTable的根基用法:
我用的IDE是NetBeans,由于也是简朴做几个页面的客户端,主要照旧Web开拓的,所以临时不规划深究java winform开拓,所以界面怎么快就怎么做了,我直接在窗体的“设计”模式下直接拖“表格”出来就了事,在默认的环境下,这个表格还会有几行几列的填充内容的,假如需要编辑,可以先点击表格进入它的编辑状态,然后右击,选择“表内容”就可以举办表格的行和列的编辑了,不外凡是环境下,表格的内容都是动态生成的,所以在控件上直接编辑它的内容的意义是不大的,虽然,做DEMO时较量有用。
奈何为JTable初始化内容呢?我的习惯是在frame的结构要领来完成,其实通过查API可以得知,JTable支持多种结构要领,而我认为动态生成数据来说,用Vector工具的形式来生成列头设置及数据设置是较量简朴直观的要领,下面来看个例子:
String[] columnModel = {"时间","编号","金额","操纵员"}; Vector cmVector = new Vector(); for(int i = 0,cmCount = columnModel.length;i < cmCount;i++){ cmVector.addElement(columnModel[i]); } //绘制数据模子 Vector dataVector = new Vector(); TempOrdersList tempOrderList = TempOrdersManager.getInstance().getList(); int billCount = tempOrderList.size(); if(billCount > 0){ for(int j = 0;j < billCount;j++){ TempOrders orderItem = (TempOrders)tempOrderList.get(j); String id = orderItem.getId(); String time = orderItem.getTime(); String no = orderItem.getNo(); float total = orderItem.getTotal(); String user = orderItem.getUser(); CustomTableCell idCell = new CustomTableCell(id,no,orderItem); Vector rowVector = new Vector(); rowVector.addElement(time); rowVector.addElement(idCell); rowVector.addElement(total); rowVector.addElement(user); dataVector.addElement(rowVector); rowVector = null; idCell = null; orderItem = null; } } DefaultTableModel tableModel = new DefaultTableModel(dataVector,cmVector){ @Override public boolean isCellEditable(int row,int column){ //只答允用户对第四列的数据举办编辑(第四列是数量) if(column == 4){ return true; }else{ return false; } } }; BillTable.setModel(tableModel); cmVector = null; dataVector = null;
利用Vector工具作为结构要领的工具时,要传入两个Vector的实例,第一个是生存数据的Vector,第二个参数是生存列头信息的Vector,在
示例中的dataVector是通过遍历一个list工具而动态填充内容的,在实例化并初始化好Vector后,只要挪用JTable的setModel要领,就可以将列头及数据的信息显示在表格中啦。
2)奈何添加行点击事件
首先,在JTable的设计视图中先选中JTable,然后右击,在事件的弹出菜单中依次选中"mouse-click”就可以捕获表格的点击事件了,在这个事件监听要领中,会传入一个java.awt.event.MouseEvent类的实例evt,用evt.getClickCount(),就可以知道触发该事件时用户的点击次数,只要这个要领返回的值大于便是2,就说明用户双击了,下面的示例代码,供各人参考:
if(evt.getClickCount() >= 2){ int rowIndex = productTable.rowAtPoint(evt.getPoint()); int columnIndex = Common.getIndetityColumn(productTable); //假如没有找到带有标志的列,就不向下执行了 if(columnIndex == -1){ return; } CustomTableCell identifyCell = (CustomTableCell)productTable.getValueAt(rowIndex, columnIndex); String productId = identifyCell.getId(); ProductDetail productDetail = new ProductDetail(productId); Common.centerWindow(productDetail); //productDetail.setVisible(true); }
#p#副标题#e#
3)奈何为JTable添加行标识
我们知道,JTable的每一个单位格,要求的值范例是一个Object的实例,也就是说,只要是一个工具就可以了,这个机制,给了我们很是大的发挥空间,我们可以在一个单位格中存放任意范例的工具,只要在这个工具中重写一下toString()要领,将我们但愿在单位格中显示的内容return出来就可以了,只JTable自己,并没有提供配置行id等识别表格行工具的要领,所以我想到了本身界说一个单位格工具,每一行中至少有一个单位格是我所界说这个工具的实例,这样,在捕获事件的时候,只要我从触发事件的行中提取到这个单位格工具出来,就办理了行标志的问题了。我界说的自界说单位格工具是这样的,有三个属性,一个是id,一个是text,尚有一个是自界说工具项,id虽然是这个行的独一标识啦,text是单位格中要显示的内容,而自界说工具项,则是为了利便在这个单位格工具中附加一个工具,利便今后挪用的,好了,看看我写的代码:
#p#分页标题#e#
查察本栏目
/** * 自界说的表格单位格工具 * @author [email protected] 2011-6-17 */ public class CustomTableCell { private String cellText = ""; private String cellId = ""; private Object customObj = null; /** * 循例都要有空结构要领的啦 */ public CustomTableCell(){ } /** * 只需初始化单位格显示内容 * @param _cellText */ public CustomTableCell(String _cellText){ this.setText(_cellText); } /** * 同时初始化单位格的id和显示文本 * @param _cellId * @param _cellText */ public CustomTableCell(String _cellId,String _cellText){ this.setText(_cellText); this.setId(_cellId); } /** * 完整参数的结构要领 * @param _cellId * @param _cellText * @param _customObj */ public CustomTableCell(String _cellId,String _cellText,Object _customObj){ this.setId(_cellId); this.setText(_cellText); this.setCustomObj(_customObj); } /** * 配置单位格显示的文本 * @param _cellText */ public void setText(String _cellText){ this.cellText = _cellText; } /** * 获取单位格显示的文本(默认为"") * @return */ public String getText(){ return this.cellText; } /** * 配置与单位格绑定的id值 * @param _cellId */ public void setId(String _cellId){ this.cellId = _cellId; } /** * 获取与单位格绑定的id值(默认为"") * @return */ public String getId(){ return this.cellId; } /** * 配置单位格附加的自界说工具(按需挪用) * @param _obj */ public void setCustomObj(Object _obj){ this.customObj = _obj; } /** * 获取单位格附加的自界说工具(默认为null) * @return */ public Object getCustomObj(){ return this.customObj; } @Override /** * 重写的要领,jtable将按照这个要领的返回值来抉择单位格显示的内容 */ public String toString(){ return this.getText(); } }
事实证明,这个类很是好用,出格是它的customObj要领,能将一个工具生存在行内的单位格中,在举办行点击响应的时候,再提取了出来,很是强大,示例:
CustomTableCell idCell = (CustomTableCell)table.getValueAt(i, idColumnIndex); float quantity = Float.parseFloat(String.valueOf(table.getValueAt(i, quantityColumnIndex))); Product product = (Product)idCell.getCustomObj();
这样,通过自界说的的单位格工具,能将整个工具获取出来,再作任何巨大的处理惩罚,都不怕啦,因为,你已经拥有所需要的一切了。
4)奈何在JTable中动态添加新行
其实这个问题很是简朴,只要按照列模子生成相应的行工具,然后通过挪用JTable的model工具的addRow要领就ok了,由于过于简朴,所以直接贴代码来说明问题:
DefaultTableModel selectionTM = (DefaultTableModel)selectionTable.getModel(); Object[] tempRow = {code,idCell,spec,price,quantity,subTotal}; selectionTM.addRow(tempRow);
嗯,没错,这样就可以新增一行了,虽然,在表格中新增一行远不止上面示例代码这么简朴的,起码,大部门环境下是需要对行标识举办比对,看是否存在要害字沟通的一行再作处理惩罚,像我所做的商品添加操纵,假如请求新添加的商品项已经存在于表格中,那么事实上只需要将表格中相应商品行的数量加一就可以了,假如之前并没存在该商品项的环境下,再举办新增一行的操纵。