Example usage for javax.swing JMenuBar JMenuBar

List of usage examples for javax.swing JMenuBar JMenuBar

Introduction

In this page you can find the example usage for javax.swing JMenuBar JMenuBar.

Prototype

public JMenuBar() 

Source Link

Document

Creates a new menu bar.

Usage

From source file:DialogTest.java

public DialogFrame() {
    setTitle("DialogTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // construct a File menu

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);// w w w.j  a  v  a  2  s . c  om
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    // add About and Exit menu items

    // The About item shows the About dialog

    JMenuItem aboutItem = new JMenuItem("About");
    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (dialog == null) // first time
                dialog = new AboutDialog(DialogFrame.this);
            dialog.setVisible(true); // pop up dialog
        }
    });
    fileMenu.add(aboutItem);

    // The Exit item exits the program

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

From source file:UndoExample5.java

public UndoExample5() {
    super("Undo/Redo Example 5");

    pane = new JTextPane();
    pane.setEditable(true); // Editable
    getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);

    // Add a menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);//  ww w .  j av  a  2s .  c  o  m

    // Populate the menu bar
    createMenuBar();

    // Create the undo manager and actions
    MonitorableUndoManager manager = new MonitorableUndoManager();
    pane.getDocument().addUndoableEditListener(manager);

    Action undoAction = new UndoAction(manager);
    Action redoAction = new RedoAction(manager);

    // Add the actions to buttons
    JPanel panel = new JPanel();
    final JButton undoButton = new JButton("Undo");
    final JButton redoButton = new JButton("Redo");
    undoButton.addActionListener(undoAction);
    redoButton.addActionListener(redoAction);

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    panel.add(undoButton);
    panel.add(redoButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    // Assign the actions to keys
    pane.registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);
    pane.registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);

    // Handle events from the MonitorableUndoManager
    manager.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            MonitorableUndoManager m = (MonitorableUndoManager) evt.getSource();
            boolean canUndo = m.canUndo();
            boolean canRedo = m.canRedo();

            undoButton.setEnabled(canUndo);
            redoButton.setEnabled(canRedo);

            undoButton.setToolTipText(canUndo ? m.getUndoPresentationName() : null);
            redoButton.setToolTipText(canRedo ? m.getRedoPresentationName() : null);
        }
    });
}

From source file:MenuTest.java

public MenuTest() {
    super();/*from   w  ww  .  j ava 2s . c  o m*/

    MenuListener listener = new MenuListener() {
        public void menuCanceled(MenuEvent e) {
            dumpInfo("Canceled", e);
        }

        public void menuDeselected(MenuEvent e) {
            dumpInfo("Deselected", e);
        }

        public void menuSelected(MenuEvent e) {
            dumpInfo("Selected", e);
        }

        private void dumpInfo(String s, MenuEvent e) {
            JMenu menu = (JMenu) e.getSource();
            System.out.println(s + ": " + menu.getText());
        }
    };

    JMenu fileMenu = new JMenu("File");
    fileMenu.addMenuListener(listener);
    fileMenu.add(new JMenuItem("Open"));
    fileMenu.add(new JMenuItem("Close"));
    fileMenu.add(new JMenuItem("Exit"));
    JMenu helpMenu = new JMenu("Help");
    helpMenu.addMenuListener(listener);
    helpMenu.add(new JMenuItem("About MenuTest"));
    helpMenu.add(new JMenuItem("Class Hierarchy"));
    helpMenu.addSeparator();
    helpMenu.add(new JCheckBoxMenuItem("Balloon Help"));
    JMenu subMenu = new JMenu("Categories");
    subMenu.addMenuListener(listener);
    JRadioButtonMenuItem rb;
    ButtonGroup group = new ButtonGroup();
    subMenu.add(rb = new JRadioButtonMenuItem("A Little Help", true));
    group.add(rb);
    subMenu.add(rb = new JRadioButtonMenuItem("A Lot of Help"));
    group.add(rb);
    helpMenu.add(subMenu);
    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(helpMenu);
    setJMenuBar(mb);
}

From source file:components.GlassPaneDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from  w w w.  j  a va 2 s .c o  m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    //Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    //Set up the glass pane, which appears over both menu bar
    //and content pane and is an item listener on the change
    //button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    //Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:MessageDigestTest.java

public MessageDigestFrame() {
    setTitle("MessageDigestTest");
    setSize(400, 200);/* w  ww. j av a2  s .c o  m*/
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JPanel panel = new JPanel();
    ButtonGroup group = new ButtonGroup();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JCheckBox b = (JCheckBox) event.getSource();
            setAlgorithm(b.getText());
        }
    };
    addCheckBox(panel, "SHA-1", group, true, listener);
    addCheckBox(panel, "MD5", group, false, listener);

    Container contentPane = getContentPane();

    contentPane.add(panel, "North");
    contentPane.add(new JScrollPane(message), "Center");
    contentPane.add(digest, "South");
    digest.setFont(new Font("Monospaced", Font.PLAIN, 12));

    setAlgorithm("SHA-1");

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenuItem fileDigestItem = new JMenuItem("File digest");
    fileDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            loadFile();
        }
    });
    menu.add(fileDigestItem);
    JMenuItem textDigestItem = new JMenuItem("Text area digest");
    textDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            String m = message.getText();
            computeDigest(m.getBytes());
        }
    });
    menu.add(textDigestItem);
    menuBar.add(menu);
    setJMenuBar(menuBar);
}

From source file:ImageViewer.java

public ImageViewerFrame() {
    setTitle("ImageViewer");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // use a label to display the images
    label = new JLabel();
    add(label);/*from w ww  .  j av a2s .c  om*/

    // set up the file chooser
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    // set up the menu bar
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu menu = new JMenu("File");
    menuBar.add(menu);

    JMenuItem openItem = new JMenuItem("Open");
    menu.add(openItem);
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // show file chooser dialog
            int result = chooser.showOpenDialog(null);

            // if file selected, set it as icon of the label
            if (result == JFileChooser.APPROVE_OPTION) {
                String name = chooser.getSelectedFile().getPath();
                label.setIcon(new ImageIcon(name));
            }
        }
    });

    JMenuItem exitItem = new JMenuItem("Exit");
    menu.add(exitItem);
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
}

From source file:com.game.ui.views.MapEditor.java

public void generateGUI() throws IOException {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    //        setResizable(false);
    JMenuBar menubar = new JMenuBar();
    ImageIcon icon = null;/*from  w  w w.java2  s.  co m*/
    try {
        icon = GameUtils.shrinkImage("save.png", 20, 20);
    } catch (IOException e) {
        System.out.println("Dialog : showDialogForMap(): Exception occured :" + e);
        e.printStackTrace();
    }
    JMenu file = new JMenu("File");
    JMenuItem save = new JMenuItem("Save", icon);
    save.setToolTipText("Save Map Information");
    save.setActionCommand("Save Map");
    save.addActionListener(this);
    file.add(save);
    menubar.add(file);
    setJMenuBar(menubar);
    JPanel topPanel = new JPanel();
    topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    topPanel.setLayout(new GridBagLayout());
    JLabel headerLbl = new JLabel("Legend : ");
    headerLbl.setFont(new Font("Times New Roman", Font.BOLD, 15));
    JLabel lbl1 = new JLabel();
    lbl1.setPreferredSize(new Dimension(50, 20));
    lbl1.setBackground(Configuration.pathColor);
    lbl1.setOpaque(true);
    JLabel lbl2 = new JLabel("- Represents the path.");
    JLabel lbl3 = new JLabel();
    lbl3.setPreferredSize(new Dimension(50, 20));
    lbl3.setBackground(Configuration.enemyColor);
    lbl3.setOpaque(true);
    JLabel lbl4 = new JLabel("- Represents the path with monsters");
    JLabel lbl5 = new JLabel();
    lbl5.setPreferredSize(new Dimension(50, 20));
    lbl5.setBackground(Configuration.startPointColor);
    lbl5.setOpaque(true);
    JLabel lbl6 = new JLabel("- Represents the starting point in the path");
    JLabel lbl7 = new JLabel();
    lbl7.setBackground(Configuration.endPointColor);
    lbl7.setOpaque(true);
    lbl7.setPreferredSize(new Dimension(50, 20));
    JLabel lbl8 = new JLabel("- Ending point in the path");
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.weightx = 1;
    c.weighty = 0;
    c.insets = new Insets(5, 5, 5, 5);
    c.gridwidth = 2;
    topPanel.add(headerLbl, c);
    c.fill = GridBagConstraints.NONE;
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 0;
    c.weighty = 0;
    c.gridwidth = 1;
    c.ipadx = 5;
    c.ipady = 5;
    topPanel.add(lbl1, c);
    c.gridx = 1;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    topPanel.add(lbl2, c);
    c.gridx = 0;
    c.gridy = 2;
    topPanel.add(lbl3, c);
    c.gridx = 1;
    topPanel.add(lbl4, c);
    c.gridx = 0;
    c.gridy = 3;
    topPanel.add(lbl5, c);
    c.gridx = 1;
    topPanel.add(lbl6, c);
    c.gridx = 0;
    c.gridy = 4;
    topPanel.add(lbl7, c);
    c.gridx = 1;
    topPanel.add(lbl8, c);
    add(topPanel, BorderLayout.NORTH);
    bottomPanel = new JPanel();
    add(bottomPanel, BorderLayout.CENTER);
    bottomPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    //        bottomPanel.add(new JButton("kaushik"));
    pack();
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    setMaximizedBounds(env.getMaximumWindowBounds());
    setVisible(true);
    callDialogForUsersInput();
}

From source file:MenuLayoutDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.PAGE_AXIS));
    menuBar.add(createMenu("Menu 1"));
    menuBar.add(createMenu("Menu 2"));
    menuBar.add(createMenu("Menu 3"));

    menuBar.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
    return menuBar;
}

From source file:org.pgptool.gui.ui.keyslist.KeysListView.java

private void initMenuBar() {
    menuBar = new JMenuBar();
    JMenu menuTs = new JMenu(Messages.get("term.actions"));
    menuTs.add(miImport = new JMenuItem());
    menuTs.add(miCreate = new JMenuItem());
    menuTs.addSeparator();// w w  w  .jav a2 s  .  co  m
    menuTs.add(miExportPublicKeys = new JMenuItem());
    menuTs.addSeparator();
    menuTs.add(miClose = new JMenuItem());
    menuBar.add(menuTs);
}

From source file:BeanContainer.java

  protected JMenuBar createMenuBar() {
  JMenuBar menuBar = new JMenuBar();
    /*w w  w.j ava 2  s  . co m*/
  JMenu mFile = new JMenu("File");

  JMenuItem mItem = new JMenuItem("New...");
  ActionListener lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {  
      Thread newthread = new Thread() {
        public void run() {
          String result = (String)JOptionPane.showInputDialog(
            BeanContainer.this, 
            "Please enter class name to create a new bean", 
            "Input", JOptionPane.INFORMATION_MESSAGE, null, 
            null, m_className);
          repaint();
          if (result==null)
            return;
          try {
            m_className = result;
            Class cls = Class.forName(result);
            Object obj = cls.newInstance();
            if (obj instanceof Component) {
              m_activeBean = (Component)obj;
              m_activeBean.addFocusListener(
                BeanContainer.this);
              m_activeBean.requestFocus();
              getContentPane().add(m_activeBean);
            }
            validate();
          }
          catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(
              BeanContainer.this, "Error: "+ex.toString(),
              "Warning", JOptionPane.WARNING_MESSAGE);
          }
        }
      };
      newthread.start();
    }
  };
  mItem.addActionListener(lst);
  mFile.add(mItem);

  mItem = new JMenuItem("Load...");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {  
      Thread newthread = new Thread() {
        public void run() {
          m_chooser.setCurrentDirectory(m_currentDir);
          m_chooser.setDialogTitle(
            "Please select file with serialized bean");
          int result = m_chooser.showOpenDialog(
            BeanContainer.this);
          repaint();
          if (result != JFileChooser.APPROVE_OPTION)
            return;
          m_currentDir = m_chooser.getCurrentDirectory();
          File fChoosen = m_chooser.getSelectedFile();
          try {
            FileInputStream fStream = 
              new FileInputStream(fChoosen);
            ObjectInput  stream  =  
              new ObjectInputStream(fStream);
            Object obj = stream.readObject();
            if (obj instanceof Component) {
              m_activeBean = (Component)obj;
              m_activeBean.addFocusListener(
                BeanContainer.this);
              m_activeBean.requestFocus();
              getContentPane().add(m_activeBean);
            }
            stream.close();
            fStream.close();
            validate();
          }
          catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(
              BeanContainer.this, "Error: "+ex.toString(),
              "Warning", JOptionPane.WARNING_MESSAGE);
          }
          repaint();
        }
      };
      newthread.start();
    }
  };
  mItem.addActionListener(lst);
  mFile.add(mItem);

  mItem = new JMenuItem("Save...");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      Thread newthread = new Thread() {
        public void run() {
          if (m_activeBean == null)
            return;
          m_chooser.setDialogTitle(
            "Please choose file to serialize bean");
          m_chooser.setCurrentDirectory(m_currentDir);
          int result = m_chooser.showSaveDialog(
            BeanContainer.this);
          repaint();
          if (result != JFileChooser.APPROVE_OPTION)
            return;
          m_currentDir = m_chooser.getCurrentDirectory();
          File fChoosen = m_chooser.getSelectedFile();
          try {
            FileOutputStream fStream = 
              new FileOutputStream(fChoosen);
            ObjectOutput stream  =  
              new ObjectOutputStream(fStream);
            stream.writeObject(m_activeBean);
            stream.close();
            fStream.close();
          }
          catch (Exception ex) {
            ex.printStackTrace();
          JOptionPane.showMessageDialog(
            BeanContainer.this, "Error: "+ex.toString(),
            "Warning", JOptionPane.WARNING_MESSAGE);
          }
        }
      };
      newthread.start();
    }
  };
  mItem.addActionListener(lst);
  mFile.add(mItem);

  mFile.addSeparator();

  mItem = new JMenuItem("Exit");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  };
  mItem.addActionListener(lst);
  mFile.add(mItem);
  menuBar.add(mFile);
    
  JMenu mEdit = new JMenu("Edit");

  mItem = new JMenuItem("Delete");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      if (m_activeBean == null)
        return;
      Object obj = m_editors.get(m_activeBean);
      if (obj != null) {
        BeanEditor editor = (BeanEditor)obj;
        editor.dispose();
        m_editors.remove(m_activeBean);
      }
      getContentPane().remove(m_activeBean);
      m_activeBean = null;
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  mEdit.add(mItem);

  mItem = new JMenuItem("Properties...");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      if (m_activeBean == null)
        return;
      Object obj = m_editors.get(m_activeBean);
      if (obj != null) {
        BeanEditor editor = (BeanEditor)obj;
        editor.setVisible(true);
        editor.toFront();
      }
      else {
        BeanEditor editor = new BeanEditor(m_activeBean);
        m_editors.put(m_activeBean, editor);
      }
    }
  };
  mItem.addActionListener(lst);
  mEdit.add(mItem);
  menuBar.add(mEdit);

  JMenu mLayout = new JMenu("Layout");
  ButtonGroup group = new ButtonGroup();

  mItem = new JRadioButtonMenuItem("FlowLayout");
  mItem.setSelected(true);
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e){
      getContentPane().setLayout(new FlowLayout());
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  group.add(mItem);
  mLayout.add(mItem);

  mItem = new JRadioButtonMenuItem("GridLayout");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e){
      int col = 3;
      int row = (int)Math.ceil(getContentPane().
        getComponentCount()/(double)col);
      getContentPane().setLayout(new GridLayout(row, col, 10, 10));
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  group.add(mItem);
  mLayout.add(mItem);
    
  mItem = new JRadioButtonMenuItem("BoxLayout - X");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      getContentPane().setLayout(new BoxLayout(
        getContentPane(), BoxLayout.X_AXIS));
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  group.add(mItem);
  mLayout.add(mItem);
    
  mItem = new JRadioButtonMenuItem("BoxLayout - Y");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      getContentPane().setLayout(new BoxLayout(
        getContentPane(), BoxLayout.Y_AXIS));
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  group.add(mItem);
  mLayout.add(mItem);
    
  mItem = new JRadioButtonMenuItem("DialogLayout");
  lst = new ActionListener() { 
    public void actionPerformed(ActionEvent e) {
      getContentPane().setLayout(new DialogLayout());
      validate();
      repaint();
    }
  };
  mItem.addActionListener(lst);
  group.add(mItem);
  mLayout.add(mItem);

  menuBar.add(mLayout);

  return menuBar;
}