Ejemplo n.º 1
0
  /**
   * Creates the menu bar
   *
   * @return Description of the Return Value
   */
  public JMenuBar createMenuBar() {
    // Create the menu bar
    final JMenuBar menuBar = new JMenuBar();

    // Menu for all beans to demo
    JMenu componentsMenu = new JMenu("Components");
    componentsMenu.setMnemonic('C');

    menuBar.add(componentsMenu);

    for (int i = 0; i < beans.length; i++) {
      Icon icon;
      JMenuItem menuItem;

      try {
        URL iconURL =
            beans[i].getClass().getResource("images/" + beans[i].getName() + "Color16.gif");
        icon = new ImageIcon(iconURL);
        menuItem = new JMenuItem(beans[i].getName(), icon);
      } catch (Exception e) {
        System.out.println(
            "JCalendarDemo.createMenuBar(): "
                + e
                + " for URL: "
                + "images/"
                + beans[i].getName()
                + "Color16.gif");
        menuItem = new JMenuItem(beans[i].getName());
      }

      componentsMenu.add(menuItem);

      final JComponent bean = beans[i];
      ActionListener actionListener =
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              installBean(bean);
            }
          };

      menuItem.addActionListener(actionListener);
    }

    // Menu for the look and feels (lnfs).
    UIManager.LookAndFeelInfo[] lnfs = UIManager.getInstalledLookAndFeels();

    ButtonGroup lnfGroup = new ButtonGroup();
    JMenu lnfMenu = new JMenu("Look&Feel");
    lnfMenu.setMnemonic('L');

    menuBar.add(lnfMenu);

    for (int i = 0; i < lnfs.length; i++) {
      if (!lnfs[i].getName().equals("CDE/Motif")) {
        JRadioButtonMenuItem rbmi = new JRadioButtonMenuItem(lnfs[i].getName());
        lnfMenu.add(rbmi);

        // preselect the current Look & feel
        rbmi.setSelected(UIManager.getLookAndFeel().getName().equals(lnfs[i].getName()));

        // store lool & feel info as client property
        rbmi.putClientProperty("lnf name", lnfs[i]);

        // create and add the item listener
        rbmi.addItemListener(
            // inlining
            new ItemListener() {
              public void itemStateChanged(ItemEvent ie) {
                JRadioButtonMenuItem rbmi2 = (JRadioButtonMenuItem) ie.getSource();

                if (rbmi2.isSelected()) {
                  // get the stored look & feel info
                  UIManager.LookAndFeelInfo info =
                      (UIManager.LookAndFeelInfo) rbmi2.getClientProperty("lnf name");

                  try {
                    menuBar.putClientProperty("jgoodies.headerStyle", "Both");
                    UIManager.setLookAndFeel(info.getClassName());

                    // update the complete application's
                    // look & feel
                    SwingUtilities.updateComponentTreeUI(JCalendarDemo.this);
                    for (int i = 0; i < beans.length; i++) {
                      SwingUtilities.updateComponentTreeUI(beans[i]);
                    }
                    // set the split pane devider border to
                    // null
                    BasicSplitPaneDivider divider =
                        ((BasicSplitPaneUI) splitPane.getUI()).getDivider();

                    if (divider != null) {
                      divider.setBorder(null);
                    }
                  } catch (Exception e) {
                    e.printStackTrace();

                    System.err.println("Unable to set UI " + e.getMessage());
                  }
                }
              }
            });
        lnfGroup.add(rbmi);
      }
    }

    // the help menu
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem aboutItem = helpMenu.add(new AboutAction(this));
    aboutItem.setMnemonic('A');
    aboutItem.setAccelerator(KeyStroke.getKeyStroke('A', java.awt.Event.CTRL_MASK));

    menuBar.add(helpMenu);

    return menuBar;
  }
  /** Creates a menu bar. */
  protected JMenuBar createMenuBar() {
    JMenu fileMenu = new JMenu("File");
    JMenuItem menuItem;

    menuItem = new JMenuItem("Open");
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser(path);
            int result = fc.showOpenDialog(frame);

            if (result == JFileChooser.APPROVE_OPTION) {
              String newPath = fc.getSelectedFile().getPath();

              new ComponentTree(newPath);
            }
          }
        });
    fileMenu.add(menuItem);
    fileMenu.addSeparator();

    menuItem = new JMenuItem("Exit");
    menuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            System.exit(0);
          }
        });
    fileMenu.add(menuItem);

    // Create a menu bar
    JMenuBar menuBar = new JMenuBar();

    menuBar.add(fileMenu);

    // Menu for the look and feels (lafs).
    UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
    ButtonGroup lafGroup = new ButtonGroup();

    JMenu optionsMenu = new JMenu("Options");

    menuBar.add(optionsMenu);

    for (int i = 0; i < lafs.length; i++) {
      JRadioButtonMenuItem rb = new JRadioButtonMenuItem(lafs[i].getName());
      optionsMenu.add(rb);
      rb.setSelected(UIManager.getLookAndFeel().getName().equals(lafs[i].getName()));
      rb.putClientProperty("UIKey", lafs[i]);
      rb.addItemListener(
          new ItemListener() {
            public void itemStateChanged(ItemEvent ae) {
              JRadioButtonMenuItem rb2 = (JRadioButtonMenuItem) ae.getSource();
              if (rb2.isSelected()) {
                UIManager.LookAndFeelInfo info =
                    (UIManager.LookAndFeelInfo) rb2.getClientProperty("UIKey");
                try {
                  UIManager.setLookAndFeel(info.getClassName());
                  SwingUtilities.updateComponentTreeUI(frame);
                } catch (Exception e) {
                  System.err.println("unable to set UI " + e.getMessage());
                }
              }
            }
          });
      lafGroup.add(rb);
    }
    return menuBar;
  }