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.

(DefaultTreeCellRenderer)jTree1.getCellRenderer();<br />

renderer.setLeafIcon(yourCus<strong>to</strong>mLeafImageIcon);<br />

renderer.setOpenIcon(yourCus<strong>to</strong>mOpenImageIcon);<br />

renderer.setClosedIcon(yourCus<strong>to</strong>mClosedImageIcon);<br />

renderer.setBackgroundSelectionColor(Color.red);<br />

NOTE: The default leaf, open icon, and closed icon are<br />

dependent on the look-and-feel. For instance, on Windows<br />

look-and-feel, the open icon is - and the closed icon is<br />

+.<br />

JTree <strong>com</strong>es with a default cell edi<strong>to</strong>r. If JTree's editable<br />

property is true, the default edi<strong>to</strong>r activates a text field for<br />

editing when the node is clicked three times. By default, this<br />

property is set <strong>to</strong> false. To create a cus<strong>to</strong>m edi<strong>to</strong>r, you need <strong>to</strong><br />

extend the DefaultCellEdi<strong>to</strong>r class, which is the same class you<br />

used in table cell editing. You can use a text field, a check<br />

box, or a <strong>com</strong>bo box, and pass it <strong>to</strong> DefaultCellEdi<strong>to</strong>r's<br />

construc<strong>to</strong>r <strong>to</strong> create an edi<strong>to</strong>r. The following code uses a <strong>com</strong>bo<br />

box for editing colors. The <strong>com</strong>bo box edi<strong>to</strong>r is shown in Figure<br />

40.30a.<br />

// Cus<strong>to</strong>mize edi<strong>to</strong>r<br />

JComboBox jcboColor = new JComboBox();<br />

jcboColor.addItem("red");<br />

jcboColor.addItem("green");<br />

jcboColor.addItem("blue");<br />

jcboColor.addItem("yellow");<br />

jcboColor.addItem("orange");<br />

jTree1.setCellEdi<strong>to</strong>r(new javax.swing.DefaultCellEdi<strong>to</strong>r(jcboColor));<br />

jTree1.setEditable(true);<br />

(a)<br />

(b)<br />

Figure 40.30<br />

You can supply a cus<strong>to</strong>m edi<strong>to</strong>r for editing tree nodes.<br />

There are two annoying problems with the edi<strong>to</strong>r created in the<br />

preceding code. First, it is activated with just one mouse click.<br />

Second, it overlaps the node's icon, as shown in Figure 40.30a.<br />

These two problems can be fixed by using the<br />

DefaultTreeCellEdi<strong>to</strong>r, as shown in the following code:<br />

jTree1.setCellEdi<strong>to</strong>r<br />

(new javax.swing.tree.DefaultTreeCellEdi<strong>to</strong>r(jTree1,<br />

new javax.swing.tree.DefaultTreeCellRenderer(),<br />

new javax.swing.DefaultCellEdi<strong>to</strong>r(jcboColor)));<br />

The new edi<strong>to</strong>r is shown in Figure 40.30b. Editing using<br />

DefaultTreeCellEdi<strong>to</strong>r starts on a triple mouse click. The <strong>com</strong>bo<br />

box does not overlap the node’s icon.<br />

49

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

Saved successfully!

Ooh no, something went wrong!