1 /*
   2  * Copyright (c) 2015, 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 import javax.swing.JFrame;
  25 import javax.swing.JMenuBar;
  26 import javax.swing.JRootPane;
  27 import javax.swing.SwingUtilities;
  28 import javax.swing.UIManager;
  29 import javax.swing.UnsupportedLookAndFeelException;
  30 
  31 import static javax.swing.UIManager.getInstalledLookAndFeels;
  32 
  33 /**
  34  * @test
  35  * @key headful
  36  * @bug 6368321
  37  * @author Sergey Bylokhov
  38  */
  39 public final class SilenceOfDeprecatedMenuBar implements Runnable {
  40 
  41     public static void main(final String[] args) throws Exception {
  42         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  43             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  44             SwingUtilities.invokeAndWait(new SilenceOfDeprecatedMenuBar());
  45         }
  46     }
  47 
  48     @Override
  49     public void run() {
  50         final JFrame frame = new DeprecatedFrame();
  51         try {
  52             final JMenuBar bar = new JMenuBar();
  53             frame.setJMenuBar(bar);
  54             frame.setBounds(100, 100, 100, 100);
  55             frame.setLocationRelativeTo(null);
  56             frame.setVisible(true);
  57             if (bar != frame.getJMenuBar()) {
  58                 throw new RuntimeException("Wrong JMenuBar");
  59             }
  60         } finally {
  61             frame.dispose();
  62         }
  63     }
  64 
  65     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
  66         try {
  67             UIManager.setLookAndFeel(laf.getClassName());
  68             System.out.println("LookAndFeel: " + laf.getClassName());
  69         } catch (ClassNotFoundException | InstantiationException |
  70                 UnsupportedLookAndFeelException | IllegalAccessException e) {
  71             throw new RuntimeException(e);
  72         }
  73     }
  74 
  75     private static class DeprecatedFrame extends JFrame {
  76 
  77         @Override
  78         protected JRootPane createRootPane() {
  79             return new JRootPane() {
  80                 @Override
  81                 public JMenuBar getMenuBar() {
  82                     throw new RuntimeException("Should not be here");
  83                 }
  84                 @Override
  85                 public void setMenuBar(final JMenuBar menu) {
  86                     throw new RuntimeException("Should not be here");
  87                 }
  88             };
  89         }
  90     }
  91 }