这个是基于AsWing组件库实现的
主要是对JTable和PropertyTableModel进行组合封装,方便使用
可以设置表格数据,列头,属性,转换器,还可以渲染单元格,在特定列中加入其它组件
package mytest.table { import org.aswing.BorderLayout; import org.aswing.JPanel; import org.aswing.JTable; import org.aswing.ListModel; import org.aswing.border.TitledBorder; import org.aswing.event.ClickCountEvent; import org.aswing.geom.IntDimension; import org.aswing.table.GeneralTableCellFactory; import org.aswing.table.PropertyTableModel; public class XPropertyTable extends JPanel { /** 数据模刑 */ private var _listModel:ListModel; /** 列名数组 */ private var _names:Array; /** 属性数组 */ private var _properties:Array; /** 单元格转换数组 */ private var _translators:Array; private var _table:JTable; private var _tableModel:PropertyTableModel; /** 双击表格处理函数 */ private var _doubleClickFunction:Function; private var _titledBorder:TitledBorder; private var _scrollPane:JScrollPane; public function XPropertyTable($listModel:ListModel, $names:Array, $properties:Array, $translators:Array, $doubleClickFunction:Function = null) { this._listModel = $listModel; this._names = $names; this._properties = $properties; this._translators = $translators; this._doubleClickFunction = $doubleClickFunction; _tableModel = new PropertyTableModel(_listModel, _names, _properties, _translators); /** 默认为不可编辑 */ _tableModel.setAllCellEditable(false); _table = new JTable(_tableModel); if($doubleClickFunction != null){ _table.addEventListener(ClickCountEvent.CLICK_COUNT, _doubleClickFunction); } _scrollPane = new JScrollPane(_table); this.setLayout(new BorderLayout()); this.append(_scrollPane, BorderLayout.CENTER); } /** 渲染列 */ public function setRenderTableCell($column:int, $cellClass:Class):void{ _table.getColumnAt($column).setCellFactory(new GeneralTableCellFactory($cellClass)); } /** 设置数据 */ public function setList($listModel:ListModel):void{ _tableModel.setList($listModel); } /** 是否可编辑 */ public function setAllCellEditable($value:Boolean = false):void{ _tableModel.setAllCellEditable($value); } /** 设置表格宽高 */ public function setTableSize($width:int, $height:int):void{ this.setPreferredSize(new IntDimension($width, $height)); } /** 设置行高 */ public function setRowHeight($value:int):void{ _table.setRowHeight($value); } /** 设置列的最大最小宽度 (可隐藏列) * * 设置它相对于默认值75 的大小,各列不同的期望宽度形成一个比例关系, * JTable 根据这个比例关系来分配初始化时各列的宽度 * * 功能说明 * 1)设置列宽度$maxWidth至$minWidth * 只能在$maxWidth至$minWidth 范围内手动调整宽度 * 如果$maxWidth=$minWidth则不能手动调整调整宽度 * * 2)隐藏列 * 当$maxWidth和$minWidth都为0时可隐藏列 * */ public function setColumnWidth($column:int, $maxWidth:int, $minWidth:int):void{ _table.getTableHeader().getColumnModel().getColumn($column).setMaxWidth($maxWidth); _table.getTableHeader().getColumnModel().getColumn($column).setMinWidth($minWidth); _table.getColumnAt($column).setMaxWidth($maxWidth); _table.getColumnAt($column).setMinWidth($minWidth); } /** 设置单元格值 */ public function setValueAt($value:Object, $row:int, $column:int):void{ _table.setValueAt($value, $row, $column); } public function setTitle($value:String):void{ _titledBorder = new TitledBorder(null, $value); this.setBorder(_titledBorder); } /** 取表格 */ public function getTable():JTable{ return _table; } /** 到表格数据模型 */ public function getTableModel():PropertyTableModel{ return _tableModel; } /** 取总行数 */ public function getRowCount():int{ return _table.getRowCount(); } /** 取总列数 */ public function getColumnCount():int{ return _table.getColumnCount(); } /** 取单元格值 */ public function getValueAt($row:int, $column:int):Object{ return _table.getValueAt($row, $column); } } }
使用
package { import flash.display.Sprite; import mytest.table.XPropertyTable; import org.aswing.AsWingManager; import org.aswing.BorderLayout; import org.aswing.JFrame; import org.aswing.JLabel; import org.aswing.JOptionPane; import org.aswing.JPanel; import org.aswing.VectorListModel; import org.aswing.event.ClickCountEvent; public class MyTest extends Sprite { private var frm:JFrame; private var mainPanel:JPanel; private var centerPanel:JPanel; private var btnPanel:JPanel; public function MyTest() { AsWingManager.initAsStandard(this); frm = new JFrame(null, "table"); frm.setSizeWH(500, 500); mainPanel = new JPanel(new BorderLayout()); var data:Array = [{name:"iiley", sex:1, age:26}, {name:"Comeny", sex:0, age:24}, {name:"Tom", sex:1, age:30}, {name:"Lita", sex:0, age:16} ]; var dataList:VectorListModel = new VectorListModel(); dataList.appendAll(data); var xpt:XPropertyTable = new XPropertyTable(dataList, ["Guy's Name", "Sex", "Age", "Oper"], ["name", "sex", "age", "."], [null, new SexTranslator(), null, null], __onDoubleClick ); xpt.setTitle("表格一"); /** 设置表格宽高 */ xpt.setTableSize(400, 400); /** 设置是否可编辑 */ xpt.setAllCellEditable(false); /** 设置行高 */ xpt.setRowHeight(30); /** 设置列宽 */ xpt.setColumnWidth(2, 50, 0); /** 渲染列 */ xpt.setRenderTableCell(3, OperateCell); mainPanel.append(new JLabel("NORTH"), BorderLayout.NORTH); var tablePanel:JPanel = new JPanel(); tablePanel.append(xpt); mainPanel.append(tablePanel, BorderLayout.CENTER); mainPanel.append(new JLabel("SOUTH"), BorderLayout.SOUTH); frm.getContentPane().setLayout(new BorderLayout()); frm.getContentPane().append(mainPanel, BorderLayout.CENTER); frm.show(); } /** 双击事件 */ private function __onDoubleClick(event:ClickCountEvent):void{ if(event.getCount() >= 2){ JOptionPane.showMessageDialog("提示", "谁双击了我!"); } } } } import org.aswing.BorderLayout; import org.aswing.Component; import org.aswing.FlowLayout; import org.aswing.JButton; import org.aswing.JPanel; import org.aswing.JTable; import org.aswing.table.PropertyTranslator; import org.aswing.table.TableCell; /** 单元格内容转换 */ class SexTranslator implements PropertyTranslator{ public function translate(info:*, key:String):*{ var sex:int = info[key] as int; if(sex == 0) return "男"; else return "女"; } } /** 渲染列 */ class OperateCell extends JPanel implements TableCell { private var _btnPanel:JPanel; private var _editBtn:JButton; protected var _value:*; private var _table:JTable; private var _selectRow:int; public function OperateCell() { initUI(); } private function initUI():void{ _btnPanel = new JPanel(new FlowLayout(2,5,1)); _editBtn = new JButton("编辑"); _btnPanel.appendAll(_editBtn); this.setLayout(new BorderLayout()); this.append(_btnPanel, BorderLayout.CENTER); } public function setCellValue($value:*):void{ } public function getCellValue():*{ return _value; } public function setTableCellStatus(table:JTable, isSelected:Boolean, row:int, column:int):void{ } public function getCellComponent() : Component { return this; } }