
482
|
Chapter 12, Miscellany
#96 Debug Components with a Custom Glass Pane
HACK
Screens and Glass
The first step is to create a sample screen for the glass pane to draw on top
of, as seen in Example 12-17.
This
main( ) method creates a frame with a few components and one nested
panel (called
panel). The ComponentGlassPane is declared as a subclass of
JComponent so it can be passed to the setGlassPane( ) method on the frame.
The glass pane is not visible initially, which produces the same behavior as if
it wasn’t even there. The activate button is used to make the glass pane
visible.
The next step is to create the
ComponentGlassPane constructor:
Example 12-17. A screen for the glass pane
public class ComponentGlassPane extends JComponent {
public static void main(String[] args) {
JFrame frame = new JFrame("Component Boundary Glasspane");
Container root = frame.getContentPane( );
root.setLayout(new BoxLayout(root,BoxLayout.Y_AXIS));
final JButton activate =
new JButton("Show component boundaries");
root.add(activate);
root.add(new JLabel("Juice Settings"));
JPanel panel = new JPanel( );
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Flavor"));
panel.add(new JTextField(" "));
root.add(panel);
frame.pack( );
frame.show( );
final ComponentGlassPane glass =
new ComponentGlassPane(frame);
frame.setGlassPane(glass);
activate.addActionListener(new ActionListener( ) {
public ...