开发工具:Eclipse3.2+VisualEditor插件
import java.awt.BorderLayout;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.FlowLayout;
// 在树节点上显示用户对象的某个字段值。
public class IDTree
{
private JFrame jFrame = null ;
private JPanel jContentPane = null ;
private JPanel jPanel = null ;
private JTree jTree = null ;
private JPanel jPanel1 = null ;
private JButton jButton = null ;
private JButton jButton1 = null ;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode root;
private int id;
/** */ /**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel()
{
if (jPanel == null )
{
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0 ;
gridBagConstraints.weightx = 1.0 ;
gridBagConstraints.weighty = 1.0 ;
gridBagConstraints.gridheight = 1 ;
gridBagConstraints.gridx = 0 ;
jPanel = new JPanel();
jPanel.setLayout( new GridBagLayout());
jPanel.add(getJTree(), gridBagConstraints);
}
return jPanel;
}
/** */ /**
* This method initializes jTree
*
* @return javax.swing.JTree
*/
private JTree getJTree()
{
if (jTree == null )
{
Person person = new Person();
root = new DefaultMutableTreeNode(person);
treeModel = new DefaultTreeModel(root);
jTree = new JTree(treeModel);
jTree.setEditable( true );
jTree.setShowsRootHandles( true );
jTree
.addTreeSelectionListener( new javax.swing.event.TreeSelectionListener()
{
public void valueChanged(
javax.swing.event.TreeSelectionEvent e)
{
// TODO Auto-generated 选择树节点事件
TreePath path = jTree.getSelectionPath();
if (path == null )
return ;
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path
.getLastPathComponent();
Person person = (Person) selectedNode
.getUserObject();
System.out.println( " ID: " + person.getId()
+ " name: " + person.getName()
+ " level: " + person.getLevel());
}
} );
}
return jTree;
}
/** */ /**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1()
{
if (jPanel1 == null )
{
jPanel1 = new JPanel();
jPanel1.setLayout( new FlowLayout());
jPanel1.add(getJButton(), null );
jPanel1.add(getJButton1(), null );
}
return jPanel1;
}
/** */ /**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton()
{
if (jButton == null )
{
jButton = new JButton();
jButton.setText( " 增加人员 " );
jButton.addActionListener( new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
// TODO Auto-generated 增加节点事件
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
if (selectedNode == null )
return ;
Person person = new Person();
person.setId( ++ id);
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(person);
treeModel.insertNodeInto(newNode, selectedNode,selectedNode.getChildCount());
TreeNode[] nodes = treeModel.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
jTree.scrollPathToVisible(path);
}
} );
}
return jButton;
}
/** */ /**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1()
{
if (jButton1 == null )
{
jButton1 = new JButton();
jButton1.setText( " 删除人员 " );
jButton1.addActionListener( new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
// TODO Auto-generated 删除节点事件
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree .getLastSelectedPathComponent();
if (selectedNode != null && selectedNode.getParent() != null )
{
treeModel.removeNodeFromParent(selectedNode);
}
}
} );
}
return jButton1;
}
/** */ /**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
IDTree application = new IDTree();
application.getJFrame().setVisible( true );
}
} );
}
/** */ /**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame()
{
if (jFrame == null )
{
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize( 800 , 600 );
jFrame.setLocation( 100 , 100 );
jFrame.setContentPane(getJContentPane());
jFrame.setTitle( " 人员级别树 " );
}
return jFrame;
}
/** */ /**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if (jContentPane == null )
{
jContentPane = new JPanel();
jContentPane.setLayout( new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.CENTER);
jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
}
return jContentPane;
}
}
class Person
{
private int id; // 人员加入顺序的编号,从1开始。
private int name; // 人员姓名,暂时不设置。
private int level; // 人员等级,暂时不设置。
public int getId()
{
return id;
}
public void setId( int id)
{
this .id = id;
}
public int getLevel()
{
return level;
}
public void setLevel( int level)
{
this .level = level;
}
public int getName()
{
return name;
}
public void setName( int name)
{
this .name = name;
}
@Override
public String toString() // 关键步骤,通过重写Object的toString(),来实现在树节点上显示加入人员的顺序编号。
{
// TODO Auto-generated method stub
return String.valueOf(id);
}
}