Java Reference
In-Depth Information
Note Classes such as JWindow , JFrame , and JApplet are heavyweight components and therefore lack
a UI delegate.
The PopupComboSample example from Chapter 13 demonstrated the creation of a new UI
delegate. Listing 20-7 is a slightly changed customized ComboBoxUI piece, where the normal
down arrow for showing the drop-down menu is replaced with a right arrow.
Listing 20-7. Customized ComboBoxUI
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
public class MyComboBoxUI extends BasicComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new MyComboBoxUI();
}
protected JButton createArrowButton() {
JButton button = new BasicArrowButton(BasicArrowButton.EAST);
return button;
}
}
To use the new UI delegate, you just need to create the class and associate it with the
component using the setUI() method.
JComboBox comboBox = new JComboBox(labels);
comboBox.setUI((ComboBoxUI)MyComboBoxUI.createUI(comboBox));
Changing the PopupComboSample from Chapter 13 (Listing 13-16) to show two combo
boxes, with the custom ComboBoxUI on the top and the bottom without, results in the screen
shown in Figure 20-6.
Figure 20-6. Using the new ComboBoxUI
 
Search WWH ::




Custom Search