MTreeDialog.java
资源名称:MapModule.rar [点击查看]
上传用户:njlgjx
上传日期:2022-08-07
资源大小:9105k
文件大小:5k
源码类别:
图形图象
开发平台:
Java
- /*
- * MTreeDialog.java
- *
- * Created on 2008年7月1日, 下午5:05
- */
- package com.mwq.map.mwing;
- import com.mwq.map.dao.Dao;
- import java.util.Iterator;
- import java.util.Vector;
- import javax.swing.JTextField;
- import javax.swing.tree.DefaultTreeModel;
- /**
- *
- * @author Administrator
- */
- public class MTreeDialog extends javax.swing.JDialog {
- private DefaultTreeModel treeModel;
- private JTextField textField;
- private Dao dao = Dao.getInstance();
- /** Creates new form MTreeDialog */
- public MTreeDialog(java.awt.Frame parent, boolean modal, JTextField textField) {
- super(parent, modal);
- this.textField = textField;
- initComponents();
- initTree();
- }
- private void initTree() {
- MTreeNode root = new MTreeNode(0, "");
- loadChildNode(root);
- treeModel = new DefaultTreeModel(root, true);
- tree.setModel(treeModel);
- }
- private void loadChildNode(MTreeNode node) {
- Vector sorts = dao.selectChildSortByFatherId(node.getId());
- for (Iterator it = sorts.iterator(); it.hasNext();) {
- Vector sort = (Vector) it.next();
- node.add(new MTreeNode((Integer) sort.get(0), sort.get(2)));
- }
- node.setLoad(true);
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- scrollPane = new javax.swing.JScrollPane();
- tree = new javax.swing.JTree();
- setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
- setUndecorated(true);
- tree.setRootVisible(false);
- tree.setShowsRootHandles(true);
- tree.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
- public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
- }
- public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
- treeTreeWillExpand(evt);
- }
- });
- tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
- public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
- treeValueChanged(evt);
- }
- });
- scrollPane.setViewportView(tree);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void treeTreeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_treeTreeWillExpand
- // TODO add your handling code here:
- MTreeNode lastNode = (MTreeNode) evt.getPath().getLastPathComponent();
- if (!lastNode.isLoad()) {
- loadChildNode(lastNode);
- treeModel.reload(lastNode);
- }
- }//GEN-LAST:event_treeTreeWillExpand
- private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
- // TODO add your handling code here:
- MTreeNode selectNode = (MTreeNode) tree.getLastSelectedPathComponent();
- textField.setText(selectNode.getUserObject().toString());
- textField.setName(selectNode.getId() + "");
- this.dispose();
- }//GEN-LAST:event_treeValueChanged
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- MTreeDialog dialog = new MTreeDialog(new javax.swing.JFrame(), true, null);
- dialog.addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowClosing(java.awt.event.WindowEvent e) {
- System.exit(0);
- }
- });
- dialog.setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JScrollPane scrollPane;
- private javax.swing.JTree tree;
- // End of variables declaration//GEN-END:variables
- }