changeset 1731:6216604c05e2

6878792: Sample provided in javax.swing.JLayer class description is not usable Reviewed-by: rupashka
author alexp
date Wed, 09 Sep 2009 17:32:18 +0400
parents d73a741a7ea1
children afd85f72784b
files src/share/classes/javax/swing/JLayer.java
diffstat 1 files changed, 63 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/JLayer.java	Tue Sep 08 14:08:59 2009 +0400
+++ b/src/share/classes/javax/swing/JLayer.java	Wed Sep 09 17:32:18 2009 +0400
@@ -56,28 +56,70 @@
  * {@code JLayer} is a good solution if you only need to do custom painting
  * over compound component or catch input events from its subcomponents.
  * <pre>
+ * import javax.swing.*;
+ * import javax.swing.plaf.LayerUI;
+ * import java.awt.*;
+ *
+ * public class JLayerSample {
+ *
+ *     private static JLayer&lt;JComponent&gt; createLayer() {
+ *         // This custom layerUI will fill the layer with translucent green
+ *         // and print out all mouseMotion events generated within its borders
+ *         LayerUI&lt;JComponent&gt; layerUI = new LayerUI&lt;JComponent&gt;() {
+ *
+ *             public void paint(Graphics g, JComponent c) {
+ *                 // paint the layer as is
+ *                 super.paint(g, c);
+ *                 // fill it with the translucent green
+ *                 g.setColor(new Color(0, 128, 0, 128));
+ *                 g.fillRect(0, 0, c.getWidth(), c.getHeight());
+ *             }
+ *
+ *             public void installUI(JComponent c) {
+ *                 super.installUI(c);
+ *                 // enable mouse motion events for the layer's subcomponents
+ *                 ((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);
+ *             }
+ *
+ *             public void uninstallUI(JComponent c) {
+ *                 super.uninstallUI(c);
+ *                 // reset the layer event mask
+ *                 ((JLayer) c).setLayerEventMask(0);
+ *             }
+ *
+ *             // overridden method which catches MouseMotion events
+ *             public void eventDispatched(AWTEvent e, JLayer&lt;? extends JComponent&gt; l) {
+ *                 System.out.println("AWTEvent detected: " + e);
+ *             }
+ *         };
  *         // create a component to be decorated with the layer
- *        JPanel panel = new JPanel();
- *        panel.add(new JButton("JButton"));
- *        // This custom layerUI will fill the layer with translucent green
- *        // and print out all mouseMotion events generated within its borders
- *        LayerUI&lt;JPanel&gt; layerUI = new LayerUI&lt;JPanel&gt;() {
- *            public void paint(Graphics g, JCompo  nent c) {
- *                // paint the layer as is
- *                super.paint(g, c);
- *                // fill it with the translucent green
- *                g.setColor(new Color(0, 128, 0, 128));
- *                g.fillRect(0, 0, c.getWidth(), c.getHeight());
- *            }
- *            // overridden method which catches MouseMotion events
- *            public void eventDispatched(AWTEvent e, JLayer&lt;JPanel&gt; l) {
- *                System.out.println("AWTEvent detected: " + e);
- *            }
- *        };
- *        // create the layer for the panel using our custom layerUI
- *        JLayer&lt;JPanel&gt; layer = new JLayer&lt;JPanel&gt;(panel, layerUI);
- *        // work with the layer as with any other Swing component
- *        frame.add(layer);
+ *         JPanel panel = new JPanel();
+ *         panel.add(new JButton("JButton"));
+ *
+ *         // create the layer for the panel using our custom layerUI
+ *         return new JLayer&lt;JComponent&gt;(panel, layerUI);
+ *     }
+ *
+ *     private static void createAndShowGUI() {
+ *         final JFrame frame = new JFrame();
+ *         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ *
+ *         // work with the layer as with any other Swing component
+ *         frame.add(createLayer());
+ *
+ *         frame.setSize(200, 200);
+ *         frame.setLocationRelativeTo(null);
+ *         frame.setVisible(true);
+ *     }
+ *
+ *     public static void main(String[] args) throws Exception {
+ *         SwingUtilities.invokeAndWait(new Runnable() {
+ *             public void run() {
+ *                 createAndShowGUI();
+ *             }
+ *         });
+ *     }
+ * }
  * </pre>
  *
  * <b>Note:</b> {@code JLayer} doesn't support the following methods: