一个构造树表格的例子,代码如下:
public JXTreeTable getCtgTreeTable() {
if (ctgTreeTable == null) {
String dbid = DataClient.RTDBOpen(currentContext, currentApp, null);
/**
* 表头
*/
Vector<String> headVector = new Vector<String>();
headVector.add("开断/越限元件描述");
headVector.add("开断/越限类型");
headVector.add("越限个数");
headVector.add("越限百分比");
headVector.add("限值");
headVector.add("计算值");
headVector.add("厂站ID");
CustomNode root = new CustomNode();// 根节点
DefaultTreeTableModel treeTableModel = new DefaultTreeTableModel(root, headVector);// 树表格模型
/**
* 安全分析开断信息属性字段
*/
Vector<String> ctgProperty = new Vector<String>();
ctgProperty.add("id");
ctgProperty.add("iddescr");
ctgProperty.add("kdtype");
ctgProperty.add("vionum");
/**
* 安全分析越限信息属性字段
*/
Vector<String> canProperty = new Vector<String>();
canProperty.add("ictgresult_id");
canProperty.add("yxdescr");
canProperty.add("yxtype");
canProperty.add("yxperlim");
canProperty.add("calimit");
canProperty.add("value");
canProperty.add("istid");
try {
Vector<Vector<DataUnit>> canVector = DataClient.RTDBRead(dbid, "canlist", canProperty);
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
format.setMaximumFractionDigits(2);// 设置小数部分的最大位数为2
/**
* 安全分析越限信息映射
*/
Map<Long, Vector<Vector<DataUnit>>> canMap = new HashMap<Long,Vector<Vector<DataUnit>>>();
for (int i = 0; i < canVector.size(); i++) {
Vector<DataUnit> data = canVector.get(i);
if (canMap.containsKey(data.get(0).getLongData())) {
canMap.get(data.get(0).getLongData()).add(data);
} else {
Vector<Vector<DataUnit>> vector = new Vector<Vector<DataUnit>>();
vector.add(data);
canMap.put(data.get(0).getLongData(), vector);
}
}
/**
* 构建树表格模型
*/
Vector<Vector<DataUnit>> ctgVector = DataClient.RTDBRead(dbid, "ctgresult", ctgProperty);
for (int i = 0; i < ctgVector.size(); i++) {
Vector<DataUnit> data = ctgVector.get(i);
if ("故障组".equals(data.get(2).getStringData())) {
/**
* 安全分析开断信息列的值
*/
Vector<String> ctgColumn = new Vector<String>();
ctgColumn.add(data.get(1).getStringData());
ctgColumn.add(data.get(2).getStringData());
ctgColumn.add("" + data.get(3).getLongData());
ctgColumn.add("");
ctgColumn.add("");
ctgColumn.add("");
ctgColumn.add("");
CustomNode ctgNode = new CustomNode(ctgColumn);
treeTableModel.insertNodeInto(ctgNode, root, i);
Vector<Vector<DataUnit>> vector = canMap.get(data.get(0).getLongData());
if (vector != null) {
for (int j = 0; j < vector.size(); j++) {
Vector<DataUnit> canData = vector.get(j);
/**
* 安全分析越限信息列的值
*/
Vector<String> canColumn = new Vector<String>();
canColumn.add(canData.get(1).getStringData());
canColumn.add(canData.get(2).getStringData());
canColumn.add("");
canColumn.add(format.format(canData.get(3).getDoubleData()));
canColumn.add(format.format(canData.get(4).getDoubleData()));
canColumn.add(format.format(canData.get(5).getDoubleData()));
canColumn.add("" + canData.get(6).getLongData());
CustomNode canNode = new CustomNode(canColumn);
treeTableModel.insertNodeInto(canNode, ctgNode, j);
}
}
}
}
} catch (MidLayerAccessException e) {
e.printStackTrace();
}
ctgTreeTable = new JXTreeTable(treeTableModel);
ctgTreeTable.setRootVisible(false);
ctgTreeTable.setShowsRootHandles(true);
/**
* 隐藏“厂站ID”列
*/
ctgTreeTable.getColumnModel().getColumn(6).setMaxWidth(0);
ctgTreeTable.getColumnModel().getColumn(6).setMinWidth(0);
ctgTreeTable.getColumnModel().getColumn(6).setPreferredWidth(0);
/**
* 设置树表格的渲染器
*/
ctgTreeTable.setTreeCellRenderer(new TreeNodeIconRenderer());
}
return ctgTreeTable;
}
其中类CustomNode代码如下:
public class CustomNode extends DefaultMutableTreeTableNode {
private Vector<String> rowData = null;// 行数据
public CustomNode() {
super();
}
public CustomNode(Vector<String> rowData) {
super(rowData);
this.rowData = rowData;
}
/**
* 得到列的个数
*/
public int getColumnCount() {
return (rowData == null) ? 0 : rowData.size();
}
/**
* 得到某列的值
*/
public Object getValueAt(int column) {
if (column < 0 || column >= getColumnCount()) {
throw new IllegalArgumentException("参数column超出索引");
}
if (rowData == null)
return null;
return rowData.get(column);
}
public Vector<String> getRowData() {
return rowData;
}
public void setRowData(Vector<String> rowData) {
this.rowData = rowData;
}
}