19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13 private JCheckBox jchkEditable = new JCheckBox();<br />

14<br />

15 // Create two but<strong>to</strong>ns<br />

16 private JBut<strong>to</strong>n jbtAdd =<br />

17 new JBut<strong>to</strong>n("Add a Child for Selected Node");<br />

18 private JBut<strong>to</strong>n jbtRemove = new JBut<strong>to</strong>n("Remove Selected Nodes");<br />

19<br />

20 // Declare two trees<br />

21 private JTree jTree1, jTree2;<br />

22<br />

23 public ModifyTree() {<br />

24 // Create the first tree<br />

25 DefaultMutableTreeNode root, europe, northAmerica, us;<br />

26<br />

27 europe = new DefaultMutableTreeNode("Europe");<br />

28 europe.add(new DefaultMutableTreeNode("UK"));<br />

29 europe.add(new DefaultMutableTreeNode("Germany"));<br />

30 europe.add(new DefaultMutableTreeNode("France"));<br />

31 europe.add(new DefaultMutableTreeNode("Norway"));<br />

32<br />

33 northAmerica = new DefaultMutableTreeNode("North America");<br />

34 us = new DefaultMutableTreeNode("US");<br />

35 us.add(new DefaultMutableTreeNode("California"));<br />

36 us.add(new DefaultMutableTreeNode("Texas"));<br />

37 us.add(new DefaultMutableTreeNode("New York"));<br />

38 us.add(new DefaultMutableTreeNode("Florida"));<br />

39 us.add(new DefaultMutableTreeNode("Illinois"));<br />

40 northAmerica.add(us);<br />

41 northAmerica.add(new DefaultMutableTreeNode("Canada"));<br />

42<br />

43 root = new DefaultMutableTreeNode("World");<br />

44 root.add(europe);<br />

45 root.add(northAmerica);<br />

46<br />

47 jcboSelectionMode.setSelectedIndex(1);<br />

48<br />

49 JPanel p1 = new JPanel();<br />

50 p1.add(new JLabel("selectionMode"));<br />

51 p1.add(jcboSelectionMode);<br />

52 p1.add(new JLabel("editable"));<br />

53 p1.add(jchkEditable);<br />

54<br />

55 JPanel p2 = new JPanel(new GridLayout(1, 2));<br />

56 p2.add(new JScrollPane(jTree1 = new JTree(root)));<br />

57 p2.add(new JScrollPane(jTree2 =<br />

58 new JTree(new DefaultTreeModel(root)))); // Same root as jTree1<br />

59<br />

60 JPanel p3 = new JPanel();<br />

61 p3.add(jbtAdd);<br />

62 p3.add(jbtRemove);<br />

63<br />

64 add(p1, BorderLayout.NORTH);<br />

65 add(p2, BorderLayout.CENTER);<br />

66 add(p3, BorderLayout.SOUTH);<br />

67<br />

68 // Register listeners<br />

69 jcboSelectionMode.addActionListener(new ActionListener() {<br />

70 @Override<br />

71 public void actionPerformed(ActionEvent e) {<br />

72 if (jcboSelectionMode.getSelectedItem().<br />

73 equals("CONTIGUOUS_TREE_SELECTION"))<br />

74 jTree1.getSelectionModel().setSelectionMode(<br />

75 TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);<br />

76 else if (jcboSelectionMode.getSelectedItem().<br />

77 equals("DISCONTIGUOUS_TREE_SELECTION"))<br />

78 jTree1.getSelectionModel().setSelectionMode(<br />

46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!