19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

All the methods related <strong>to</strong> path selection are also defined in the<br />

TreeSelectionModel interface, which will be covered in §40.12,<br />

“TreePath and TreeSelectionModel.”<br />

Listing 40.12 gives an example that creates four trees: a default<br />

tree using the no-arg construc<strong>to</strong>r, a tree created from an array<br />

of objects, a tree created from a vec<strong>to</strong>r, and a tree created from<br />

a hash table, as shown in Figure 40.21. Enable the user <strong>to</strong><br />

dynamically set the properties for rootVisible, rowHeight, and<br />

showsRootHandles.<br />

Figure 40.21<br />

You can dynamically set the properties for rootVisible, rowHeight,<br />

and showRootHandles in a tree.<br />

Listing 40.12 SimpleTreeDemo.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

1 import java.awt.*;<br />

2 import java.awt.event.*;<br />

3 import javax.swing.*;<br />

4 import javax.swing.event.*;<br />

5 import java.util.*;<br />

6<br />

7 public class SimpleTreeDemo extends JApplet {<br />

8 // Create a default tree<br />

9 private JTree jTree1 = new JTree();<br />

10<br />

11 // Create a tree with an array of Objects.<br />

12 private JTree jTree2 = new JTree(new String[]<br />

13 {"dog", "cow", "cat", "pig", "rabbit"});<br />

14<br />

15 // Create a tree with a Hashtable<br />

16 private Vec<strong>to</strong>r vec<strong>to</strong>r = new Vec<strong>to</strong>r(Arrays.asList(<br />

17 new Object[]{"red", "green", "black", "white", "purple"}));<br />

18 private JTree jTree3 = new JTree(vec<strong>to</strong>r);<br />

19<br />

20 private Hashtable hashtable =<br />

21 new Hashtable();<br />

22 private JTree jTree4;<br />

23<br />

24 // Create a <strong>com</strong>bo box for selecting rootVisible<br />

25 private JComboBox jcboRootVisible = new JComboBox(<br />

26 new String[]{"false", "true"});<br />

27<br />

28 // Create a <strong>com</strong>bo box for selecting showRootHandles<br />

33

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

Saved successfully!

Ooh no, something went wrong!