/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.*; 

2. import javax.swing.event.*; 

3. import javax.swing.tree.*; 

4. import java.awt.*; 

5.  

6. public class Tree 

7.     extends JApplet 

8.     implements  TreeSelectionListener 

9.

10.   JTree tr ; 

11.   JScrollPane sp ; 

12.  

13.   public void valueChanged ( TreeSelectionEvent ev ) 

14.   { 

15.     String sel = tr.getSelectionPath().toString() ; 

16.     tf.setText( sel ); 

17.   } 

18.  

19.  

20.   public void init() 

21.   { 

22.     DefaultMutableTreeNode stem = 

23.       new DefaultMutableTreeNode("StarTricks"); 

24.  

25.     DefaultMutableTreeNode branch ; 

26.  

27.     branch = new DefaultMutableTreeNode  ( "Voyeur" ) ; 

28.  

29.     branch.add ( 

30.       new DefaultMutableTreeNode("SoixanteDeNeuf")  ); 

31.     branch.add ( 

32.       new DefaultMutableTreeNode ( "ChuckyTea" )  ); 

33.     branch.insert ( 

34.       new DefaultMutableTreeNode("Capt.Chainplay"), 0 ); 

35.  

36.     stem.add ( branch ) ; 

37.  

38.     branch = new DefaultMutableTreeNode  ( "WeepSpaceFine" ) ; 

39.  

40.     branch.add ( 

41.       new DefaultMutableTreeNode ( "Capt.SixPacko" )  ); 

42.     branch.add ( 

43.       new DefaultMutableTreeNode ( "Woof" )  ); 

44.     branch.add ( 

45.       new DefaultMutableTreeNode ( "Keerie Neureasy" )  ); 

46.  

47.     stem.add ( branch ) ; 

48.  

49.     tr = new JTree ( stem ); 

50.  

51.     tr.addTreeSelectionListener ( this ) ; 

52.  

53.     sp = new JScrollPane( tr ) ; 

54.     sp.setPreferredSize( new Dimension( 333, 250 )  ); 

55.     cp = getContentPane( ) ; 

56.     cp.setLayout ( new FlowLayout( )  ); 

57.     cp.add ( sp ); 

58.     cp.add ( tf ); 

59.   } 

60.   JTextField tf = new JTextField ( 30 ) ; 

61.   Container cp ; 

62.

63. /*====  various : ======== 

64. stem.remove( somenode ) 

65. branch.remove( somenode ) 

66. branch.removeAllChildren() 

67. */ 

68.