今天主要是将如下界面做成JAVA版的,我做这个Winform的目的并不是说觉得winform有前途,而是在练手,为了后面web和Andriod开发打点基础。
那做好的java界面如下
OK,画好了,看代码,首先是绑定树的一个代码
public void BuildCodeTree() throws SQLException { treeCode.removeAll(); DefaultMutableTreeNode top = new DefaultMutableTreeNode(new NodeData( "root", "系统参数")); String sql = "SELECT Distinct ename,cname FROM dbo.Codes WITH(NOLOCK)"; ResultSet res = JDBCSqlHelper.query(sql); try { while (res.next()) { DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode( new NodeData(res.getString("ename"), res.getString("cname"))); top.add(childTreeNode); } this.treeCode = new JTree(top); treeCode.setBounds(10, 39, 146, 278); treeCode.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeCode .getLastSelectedPathComponent(); String nodeName = ((NodeData) node.getUserObject()).ename; if (nodeName == "root") return; String sql = "SELECT 0 as bit,data,ename,cname,display_content FROM dbo.Codes WHERE ename='" + nodeName + "'"; ResultSet res = JDBCSqlHelper.query(sql); ListcolumnList = new ArrayList (); columnList.add("选择"); columnList.add("数据值"); columnList.add("英文代码"); columnList.add("中文代码"); columnList.add("显示值"); DataFillHelper.FillTable(res, table, columnList); table.getColumnModel().getColumn(0) .setCellRenderer(new TableCellRenderer() { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JCheckBox ck = new JCheckBox(); ck.setSelected(isSelected); ck.requestFocus(hasFocus); ck.setHorizontalAlignment((int) 0.5f); return ck; } }); table.getColumnModel().getColumn(0).setWidth(10); } }); treeCode.setBorder(new LineBorder(new Color(0, 0, 0))); contentPane.add(treeCode); // treeCode.setCellRenderer(new MyRenderer()); treeCode.setShowsRootHandles(true); treeCode.setRootVisible(true); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { res.close(); } }
这段代码首先会从数据库读出要加载的树的数据,由于这里的树只有两级,所以代码很简单。先定义一个root节点,然后将数库中的读出的节点加载在他下面。这样树就形成了,然后在树节点被选择时注册一个事件,事件中,如果选择的是root节点,则不加载右边的Jtable数据。否则根据英文代码加载左边的Jtable数据。
节点的数据使用NodeData这个类类记录的
private class NodeData { private String ename; public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } private String cname; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public NodeData() { } public NodeData(String ename, String cname) { this.setEname(ename); this.setCname(cname); } public String toString() { return cname; } }
左边的JTable数据的加载通过DataFillHelper类实现。
public class DataFillHelper { public static void FillTable(ResultSet res, JTable jTable,ListcolumnList) { Vector columnHeads = new Vector (); Vector rows = new Vector (); try { ResultSetMetaData rsmd = res.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { columnHeads.addElement(rsmd.getColumnName(i)); } while (res.next()) { Vector v = new Vector (); for (int i = 1; i <= rsmd.getColumnCount(); i++) { v.addElement(res.getString(i)); } rows.add(v); } DefaultTableModel model = new DefaultTableModel(rows, columnHeads); jTable.setModel(model); makeFace(jTable); for (int i = 0; i < jTable.getColumnModel().getColumnCount(); i++) { jTable.getColumnModel().getColumn(i).setHeaderValue( columnList.get(i)); } } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public static void makeFace(JTable table) { try { DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (row % 2 == 0) setBackground(new Color(206, 231, 255)); else if (row % 2 == 1) setBackground(Color.white); return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } }; for (int i = 0; i < table.getColumnCount(); i++) { table.getColumn(table.getColumnName(i)).setCellRenderer(tcr); } } catch (Exception ex) { ex.printStackTrace(); } } }
这里就不多做解释,解释我也解释不清楚。OK,在这里需要说明的是如果将jtable放在JScrollPane中,可能会导致jtable的列头不能显示,所以需要加入下面的代码
table = new JTable(); table.setBorder(UIManager.getBorder("FormattedTextField.border")); table.setBackground(Color.WHITE); JScrollPane scrollPane = new JScrollPane(table); scrollPane.setBounds(166, 39, 430, 278); contentPane.add(scrollPane); table.setFillsViewportHeight(true);
就是那句SetFillsViewportHeight。OK,接下来我们看一下界面上的删除按钮功能。在前面我们已经在JTable中加入了checkBox。接下里我们看删除按钮的代码
private void DeleteCodes() { int[] rowIndexs = table.getSelectedRows(); if(rowIndexs.length==0){ MessageHelper.ShowMessage("请选择要删除的数据!"); return; } StringBuffer strBuffer = new StringBuffer(); for (int i : rowIndexs) { Object data = table.getValueAt(i, 1); Object ename = table.getValueAt(i, 2); strBuffer.append("'"); strBuffer.append(data.toString() + ename.toString()); strBuffer.append("',"); } strBuffer.setLength(strBuffer.length() - 1); String sql = "DELETE FROM dbo.Codes WHERE (data+ename) IN (" + strBuffer.toString() + ")"; MessageHelper.ShowMessage(sql); JDBCSqlHelper.update(sql); }
还有一个要说的是,我们并不想让后后面的列被编辑,我们只想第一列被编辑,所以需要覆盖IsCellEditable方法
table = new JTable() { public boolean isCellEditable(int row, int column) { if (column == 0) { return true; } else return false; } };
OK,今天就讲到这里,洗洗睡!