|
| 1 | +/* |
| 2 | + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * - Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * - Neither the name of Oracle or the names of its |
| 16 | + * contributors may be used to endorse or promote products derived |
| 17 | + * from this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package test.components; |
| 33 | + |
| 34 | +import javax.swing.*; |
| 35 | +import java.awt.*; |
| 36 | +import java.awt.event.ActionEvent; |
| 37 | +import java.awt.event.ActionListener; |
| 38 | +import java.awt.event.ItemEvent; |
| 39 | +import java.awt.event.ItemListener; |
| 40 | +import java.awt.event.MouseEvent; |
| 41 | +import javax.swing.event.MouseInputAdapter; |
| 42 | + |
| 43 | +/** An application that requires no other files. */ |
| 44 | +public class GlassPaneDemo { |
| 45 | + static private MyGlassPane myGlassPane; |
| 46 | + |
| 47 | + static int b1i = 0, b2i = 0; |
| 48 | + |
| 49 | + /** |
| 50 | + * Create the GUI and show it. For thread safety, this method should be invoked |
| 51 | + * from the event-dispatching thread. |
| 52 | + */ |
| 53 | + private static void createAndShowGUI() { |
| 54 | + // Create and set up the window. |
| 55 | + JFrame frame = new JFrame("GlassPaneDemo"); |
| 56 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 57 | + |
| 58 | + // Start creating and adding components. |
| 59 | + JCheckBox changeButton = new JCheckBox("Glass pane \"visible\""); |
| 60 | + changeButton.setSelected(false); |
| 61 | + |
| 62 | + // Set up the content pane, where the "main GUI" lives. |
| 63 | + Container contentPane = frame.getContentPane(); |
| 64 | + contentPane.setLayout(new FlowLayout()); |
| 65 | + contentPane.add(changeButton); |
| 66 | + JButton b1 = new JButton("Button 1"); |
| 67 | + JButton b2 = new JButton("Button 2"); |
| 68 | + contentPane.add(b1); |
| 69 | + contentPane.add(b2); |
| 70 | + b1.addActionListener(new ActionListener() { |
| 71 | + |
| 72 | + @Override |
| 73 | + public void actionPerformed(ActionEvent e) { |
| 74 | + setBColor(b1); |
| 75 | + } |
| 76 | + |
| 77 | + }); |
| 78 | + b2.addActionListener(new ActionListener() { |
| 79 | + |
| 80 | + @Override |
| 81 | + public void actionPerformed(ActionEvent e) { |
| 82 | + setBColor(b2); |
| 83 | + } |
| 84 | + |
| 85 | + }); |
| 86 | + // Set up the menu bar, which appears above the content pane. |
| 87 | + JMenuBar menuBar = new JMenuBar(); |
| 88 | + JMenu menu = new JMenu("Menu"); |
| 89 | + menu.add(new JMenuItem("Do nothing")); |
| 90 | + menuBar.add(menu); |
| 91 | + frame.setJMenuBar(menuBar); |
| 92 | + |
| 93 | + // Set up the glass pane, which appears over both menu bar |
| 94 | + // and content pane and is an item listener on the change |
| 95 | + // button. |
| 96 | + myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane()); |
| 97 | + changeButton.addItemListener(myGlassPane); |
| 98 | + frame.setGlassPane(myGlassPane); |
| 99 | + |
| 100 | + // Show the window. |
| 101 | + frame.pack(); |
| 102 | + frame.setVisible(true); |
| 103 | + } |
| 104 | + |
| 105 | + protected static void setBColor(JButton b) { |
| 106 | + b.setForeground((b1i++) % 2 == 1 ? Color.red : Color.blue); |
| 107 | + } |
| 108 | + |
| 109 | + public static void main(String[] args) { |
| 110 | + // Schedule a job for the event-dispatching thread: |
| 111 | + // creating and showing this application's GUI. |
| 112 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 113 | + public void run() { |
| 114 | + createAndShowGUI(); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * We have to provide our own glass pane so that it can paint. |
| 122 | + */ |
| 123 | +class MyGlassPane extends JComponent implements ItemListener { |
| 124 | + Point point; |
| 125 | + |
| 126 | + // React to change button clicks. |
| 127 | + public void itemStateChanged(ItemEvent e) { |
| 128 | + setVisible(e.getStateChange() == ItemEvent.SELECTED); |
| 129 | + } |
| 130 | + |
| 131 | + protected void paintComponent(Graphics g) { |
| 132 | + if (point != null) { |
| 133 | + g.setColor(Color.red); |
| 134 | + g.fillOval(point.x - 10, point.y - 10, 20, 20); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public void setPoint(Point p) { |
| 139 | + point = p; |
| 140 | + } |
| 141 | + |
| 142 | + public MyGlassPane(AbstractButton aButton, JMenuBar menuBar, Container contentPane) { |
| 143 | + CBListener listener = new CBListener(aButton, menuBar, this, contentPane); |
| 144 | + addMouseListener(listener); |
| 145 | + addMouseMotionListener(listener); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Listen for all events that our check box is likely to be interested in. |
| 151 | + * Redispatch them to the check box. |
| 152 | + */ |
| 153 | +class CBListener extends MouseInputAdapter { |
| 154 | + Toolkit toolkit; |
| 155 | + Component liveButton; |
| 156 | + JMenuBar menuBar; |
| 157 | + MyGlassPane glassPane; |
| 158 | + Container contentPane; |
| 159 | + |
| 160 | + public CBListener(Component liveButton, JMenuBar menuBar, MyGlassPane glassPane, Container contentPane) { |
| 161 | + toolkit = Toolkit.getDefaultToolkit(); |
| 162 | + this.liveButton = liveButton; |
| 163 | + this.menuBar = menuBar; |
| 164 | + this.glassPane = glassPane; |
| 165 | + this.contentPane = contentPane; |
| 166 | + } |
| 167 | + |
| 168 | + public void mouseMoved(MouseEvent e) { |
| 169 | + redispatchMouseEvent(e, false); |
| 170 | + } |
| 171 | + |
| 172 | + public void mouseDragged(MouseEvent e) { |
| 173 | + redispatchMouseEvent(e, false); |
| 174 | + } |
| 175 | + |
| 176 | + public void mouseClicked(MouseEvent e) { |
| 177 | + redispatchMouseEvent(e, false); |
| 178 | + } |
| 179 | + |
| 180 | + public void mouseEntered(MouseEvent e) { |
| 181 | + redispatchMouseEvent(e, false); |
| 182 | + } |
| 183 | + |
| 184 | + public void mouseExited(MouseEvent e) { |
| 185 | + redispatchMouseEvent(e, false); |
| 186 | + } |
| 187 | + |
| 188 | + public void mousePressed(MouseEvent e) { |
| 189 | + redispatchMouseEvent(e, false); |
| 190 | + } |
| 191 | + |
| 192 | + public void mouseReleased(MouseEvent e) { |
| 193 | + redispatchMouseEvent(e, true); |
| 194 | + } |
| 195 | + |
| 196 | + // A basic implementation of redispatching events. |
| 197 | + private void redispatchMouseEvent(MouseEvent e, boolean repaint) { |
| 198 | + Point glassPanePoint = e.getPoint(); |
| 199 | + Container container = contentPane; |
| 200 | + Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane); |
| 201 | + if (containerPoint.y < 0) { // we're not in the content pane |
| 202 | + if (containerPoint.y + menuBar.getHeight() >= 0) { |
| 203 | + // The mouse event is over the menu bar. |
| 204 | + // Could handle specially. |
| 205 | + } else { |
| 206 | + // The mouse event is over non-system window |
| 207 | + // decorations, such as the ones provided by |
| 208 | + // the Java look and feel. |
| 209 | + // Could handle specially. |
| 210 | + } |
| 211 | + } else { |
| 212 | + // The mouse event is probably over the content pane. |
| 213 | + // Find out exactly which component it's over. |
| 214 | + Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y); |
| 215 | + |
| 216 | + if ((component != null) && (component.equals(liveButton))) { |
| 217 | + // Forward events over the check box. |
| 218 | + Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component); |
| 219 | + component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), |
| 220 | + componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + // Update the glass pane if requested. |
| 225 | + if (repaint) { |
| 226 | + glassPane.setPoint(glassPanePoint); |
| 227 | + glassPane.repaint(); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments