< prev index next >

test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug 4634626
  28  * @summary Implement context popup menus for components
  29  * @author Alexander Zuev
  30  * @library ../../../../lib/testlibrary
  31  * @build ExtendedRobot
  32  * @run applet bug4634626.html
  33  */
  34 
  35 import javax.swing.*;
  36 import java.awt.event.*;
  37 import java.awt.*;
  38 
  39 public class bug4634626 extends JApplet {
  40 
  41     public boolean passed = true;
  42     public boolean done = false;
  43 
  44     public JFrame mainFrame = new JFrame("Bug4634626");
  45     public JRootPane rootPane = mainFrame.getRootPane();
  46     public JPanel contentPane = new JPanel();
  47     public JButton nopButton = new JButton("No popup button");
  48     public JTextArea someText = new JTextArea("Some text here", 20, 10);
  49     public JButton popButton = new JButton("Button with the popup");
  50 
  51     public JPopupMenu btnPopup = new JPopupMenu();
  52     public JPopupMenu commonPopup = new JPopupMenu();
  53     static public Error toBeThrown = null;
  54     static int popTrig = MouseEvent.BUTTON3_MASK;
  55     static boolean popt = false;
  56 
  57     public static class MouseWatcher extends MouseAdapter {
  58         public void mousePressed(MouseEvent e) {
  59             if(e.isPopupTrigger()) popt = true;


  67             }
  68         }
  69         public void mouseReleased(MouseEvent e) {
  70             if(e.isPopupTrigger()) popt = true;
  71             if(e.getComponent() != null &&
  72                e.getComponent() instanceof JComponent &&
  73                e.isPopupTrigger() &&
  74                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  75                 toBeThrown =
  76                   new Error("The event got thru the component with popup: "
  77                   + e);
  78             }
  79             if(toBeThrown != null) {
  80                 throw(toBeThrown);
  81             }
  82         }
  83     }
  84 
  85     public static MouseWatcher mouser = new MouseWatcher();
  86 






  87     public void init() {
  88 
  89         try {
  90             popButton.setComponentPopupMenu(null);
  91             popButton.setComponentPopupMenu(null);
  92             popButton.setComponentPopupMenu(btnPopup);
  93             popButton.setComponentPopupMenu(null);
  94         } catch(Exception ex) {
  95             System.err.println("Unexpected exception was thrown by " +
  96                                "setComponentPopupMenu() method: " + ex);
  97         }
  98         btnPopup.add("Button 1");
  99         btnPopup.add("Button 2");
 100         btnPopup.add("Button 3");
 101         popButton.setComponentPopupMenu(btnPopup);
 102         popButton.addMouseListener(mouser);
 103         commonPopup.add("One");
 104         commonPopup.add("Two");
 105         commonPopup.add("Three");
 106 
 107         contentPane.setLayout(new BorderLayout());
 108         contentPane.setComponentPopupMenu(commonPopup);
 109         contentPane.addMouseListener(mouser);
 110         contentPane.add(nopButton, BorderLayout.NORTH);
 111         nopButton.addMouseListener(mouser);
 112         contentPane.add(popButton, BorderLayout.SOUTH);
 113         someText.addMouseListener(mouser);
 114         contentPane.add(someText, BorderLayout.CENTER);
 115         mainFrame.setContentPane(contentPane);
 116 
 117         mainFrame.pack();
 118         mainFrame.setLocation(50, 50);
 119 
 120         mainFrame.addWindowListener(new TestStateListener());

 121         mainFrame.setVisible(true);
 122 
 123         while(!done) Thread.yield();
 124 
 125         if(!passed) {
 126             throw new RuntimeException("Test failed");
 127         }
 128 
 129     }
 130 
 131     public class TestStateListener extends WindowAdapter {
 132         public void windowOpened(WindowEvent ev) {
 133             try {
 134                 ev.getWindow().toFront();
 135                 ev.getWindow().requestFocus();
 136                 new Thread(new RobotThread()).start();
 137             } catch (Exception ex) {
 138                 throw new RuntimeException("Thread Exception");
 139             }
 140         }


   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug 4634626
  28  * @summary Implement context popup menus for components

  29  * @library ../../../../lib/testlibrary
  30  * @build ExtendedRobot
  31  * @run main bug4634626
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.event.*;
  36 import java.awt.*;
  37 
  38 public class bug4634626 {
  39 
  40     public boolean passed = true;
  41     public boolean done = false;
  42 
  43     public JFrame mainFrame = new JFrame("Bug4634626");
  44     public JRootPane rootPane = mainFrame.getRootPane();
  45     public JPanel contentPane = new JPanel();
  46     public JButton nopButton = new JButton("No popup button");
  47     public JTextArea someText = new JTextArea("Some text here", 20, 10);
  48     public JButton popButton = new JButton("Button with the popup");
  49 
  50     public JPopupMenu btnPopup = new JPopupMenu();
  51     public JPopupMenu commonPopup = new JPopupMenu();
  52     static public Error toBeThrown = null;
  53     static int popTrig = MouseEvent.BUTTON3_MASK;
  54     static boolean popt = false;
  55 
  56     public static class MouseWatcher extends MouseAdapter {
  57         public void mousePressed(MouseEvent e) {
  58             if(e.isPopupTrigger()) popt = true;


  66             }
  67         }
  68         public void mouseReleased(MouseEvent e) {
  69             if(e.isPopupTrigger()) popt = true;
  70             if(e.getComponent() != null &&
  71                e.getComponent() instanceof JComponent &&
  72                e.isPopupTrigger() &&
  73                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  74                 toBeThrown =
  75                   new Error("The event got thru the component with popup: "
  76                   + e);
  77             }
  78             if(toBeThrown != null) {
  79                 throw(toBeThrown);
  80             }
  81         }
  82     }
  83 
  84     public static MouseWatcher mouser = new MouseWatcher();
  85 
  86     public static void main(final String[] args) {
  87         bug4634626 app = new bug4634626();
  88         app.init();
  89         app.destroy();
  90     }
  91 
  92     public void init() {
  93 
  94         try {
  95             popButton.setComponentPopupMenu(null);
  96             popButton.setComponentPopupMenu(null);
  97             popButton.setComponentPopupMenu(btnPopup);
  98             popButton.setComponentPopupMenu(null);
  99         } catch(Exception ex) {
 100             System.err.println("Unexpected exception was thrown by " +
 101                                "setComponentPopupMenu() method: " + ex);
 102         }
 103         btnPopup.add("Button 1");
 104         btnPopup.add("Button 2");
 105         btnPopup.add("Button 3");
 106         popButton.setComponentPopupMenu(btnPopup);
 107         popButton.addMouseListener(mouser);
 108         commonPopup.add("One");
 109         commonPopup.add("Two");
 110         commonPopup.add("Three");
 111 
 112         contentPane.setLayout(new BorderLayout());
 113         contentPane.setComponentPopupMenu(commonPopup);
 114         contentPane.addMouseListener(mouser);
 115         contentPane.add(nopButton, BorderLayout.NORTH);
 116         nopButton.addMouseListener(mouser);
 117         contentPane.add(popButton, BorderLayout.SOUTH);
 118         someText.addMouseListener(mouser);
 119         contentPane.add(someText, BorderLayout.CENTER);
 120         mainFrame.setContentPane(contentPane);
 121 
 122         mainFrame.pack();
 123         mainFrame.setLocation(50, 50);
 124 
 125         mainFrame.addWindowListener(new TestStateListener());
 126         mainFrame.setLocationRelativeTo(null);
 127         mainFrame.setVisible(true);
 128 
 129         while(!done) Thread.yield();
 130 
 131         if(!passed) {
 132             throw new RuntimeException("Test failed");
 133         }
 134 
 135     }
 136 
 137     public class TestStateListener extends WindowAdapter {
 138         public void windowOpened(WindowEvent ev) {
 139             try {
 140                 ev.getWindow().toFront();
 141                 ev.getWindow().requestFocus();
 142                 new Thread(new RobotThread()).start();
 143             } catch (Exception ex) {
 144                 throw new RuntimeException("Thread Exception");
 145             }
 146         }


< prev index next >