Example usage for javax.swing JMenuBar add

List of usage examples for javax.swing JMenuBar add

Introduction

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

Prototype

public JMenu add(JMenu c) 

Source Link

Document

Appends the specified menu to the end of the menu bar.

Usage

From source file:processing.app.Editor.java

protected void buildMenuBar() throws Exception {
    JMenuBar menubar = new JMenuBar();
    final JMenu fileMenu = buildFileMenu();
    fileMenu.addMenuListener(new StubMenuListener() {
        @Override//  ww w . j  ava2  s  .c om
        public void menuSelected(MenuEvent e) {
            List<Component> components = Arrays.asList(fileMenu.getComponents());
            if (!components.contains(sketchbookMenu)) {
                fileMenu.insert(sketchbookMenu, 2);
            }
            if (!components.contains(sketchbookMenu)) {
                fileMenu.insert(examplesMenu, 3);
            }
            fileMenu.revalidate();
            validate();
        }
    });
    menubar.add(fileMenu);

    menubar.add(buildEditMenu());

    final JMenu sketchMenu = new JMenu(_("Sketch"));
    sketchMenu.addMenuListener(new StubMenuListener() {

        @Override
        public void menuSelected(MenuEvent e) {
            buildSketchMenu(sketchMenu);
            sketchMenu.revalidate();
            validate();
        }
    });
    buildSketchMenu(sketchMenu);
    menubar.add(sketchMenu);

    final JMenu toolsMenu = buildToolsMenu();
    toolsMenu.addMenuListener(new StubMenuListener() {
        @Override
        public void menuSelected(MenuEvent e) {
            List<Component> components = Arrays.asList(fileMenu.getComponents());
            int offset = 0;
            for (JMenu menu : base.getBoardsCustomMenus()) {
                if (!components.contains(menu)) {
                    toolsMenu.insert(menu, numTools + offset);
                    offset++;
                }
            }
            if (!components.contains(serialMenu)) {
                toolsMenu.insert(serialMenu, numTools + offset);
            }
            toolsMenu.revalidate();
            validate();
        }
    });
    menubar.add(toolsMenu);

    menubar.add(buildHelpMenu());
    setJMenuBar(menubar);
}

From source file:psidev.psi.mi.tab.client.gui.DragAndDropConverter.java

public static void addFileMenu(final JFrame frame, JMenuBar menuBar) {

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

    // Create a file open iten
    JMenuItem openFileItem = new JMenuItem("Open files...");
    openFileItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFileChooser chooser = new JFileChooser();

            chooser.setApproveButtonText("Convert to MITAB25");

            // Enable multiple selections
            chooser.setMultiSelectionEnabled(true);

            // Show the dialog; wait until dialog is closed
            chooser.showOpenDialog(frame);

            // Retrieve the selected files. This method returns empty
            // if multiple-selection mode is not enabled.
            File[] files = chooser.getSelectedFiles();

            if (files != null && files.length > 0) {
                FilesProcessor processor = new FilesProcessor();
                processor.process(frame, files, expansionStrategy, postProcessorStrategy,
                        aggregateSelectedFiles);
            }/*from  ww  w . j  a v a  2  s . com*/
        }
    });
    menu.add(openFileItem);

    // Create an exit
    final JMenuItem mergeItem = new JCheckBoxMenuItem("Aggregate all selected files", aggregateSelectedFiles);
    mergeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // If selected, all files dropped or selected at once are aggregated into a single MITAB file
            aggregateSelectedFiles = mergeItem.isSelected();
            log.debug("Aggregate is " + aggregateSelectedFiles);
        }
    });
    menu.add(mergeItem);

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

From source file:psidev.psi.mi.tab.client.gui.DragAndDropConverter.java

public static void addExpansionMenu(JMenuBar menuBar) {

    ButtonGroup itemGroup = new ButtonGroup();

    // Create a menu
    JMenu menu = new JMenu("Expansion model");
    menuBar.add(menu);

    // Create none item
    JMenuItem noneItem = new JRadioButtonMenuItem("None");
    noneItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.debug("No expansion strategy selected.");
            expansionStrategy = null;//from   w  w  w .  j a  va2  s .  c om
        }
    });
    itemGroup.add(noneItem);
    menu.add(noneItem);

    // Create spoke model item
    JMenuItem spokeItem = new JRadioButtonMenuItem("Spoke");
    spokeItem.setSelected(true);
    spokeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.debug("Sets expansion statefy to Spoke.");
            expansionStrategy = new SpokeExpansion();
        }
    });
    itemGroup.add(spokeItem);
    menu.add(spokeItem);

    // Create an exit
    JMenuItem matrixItem = new JRadioButtonMenuItem("Matrix");
    matrixItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.debug("Sets expansion statefy to Matrix.");
            expansionStrategy = new MatrixExpansion();
        }
    });
    itemGroup.add(matrixItem);
    menu.add(matrixItem);
}

From source file:psidev.psi.mi.tab.client.gui.DragAndDropConverter.java

public static void addProcessorMenu(JMenuBar menuBar) {

    // Create a menu
    JMenu menu = new JMenu("Processing");
    menuBar.add(menu);

    ButtonGroup itemGroup = new ButtonGroup();

    // Create none item
    JMenuItem noneItem = new JRadioButtonMenuItem("None");
    noneItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.debug("Disables post processing.");
            postProcessorStrategy = null;
        }/*www.  j a v a 2 s .  c  o m*/
    });
    itemGroup.add(noneItem);
    menu.add(noneItem);

    // Create a clustering item
    JMenuItem clusteringInteractorItem = new JRadioButtonMenuItem("Clustering interactor pairs");
    clusteringInteractorItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.debug("Enables post processing: ClusterPerInteractorProcessor");
            postProcessorStrategy = new ClusterInteractorPairProcessor();
        }
    });
    itemGroup.add(clusteringInteractorItem);
    clusteringInteractorItem.setSelected(true);
    menu.add(clusteringInteractorItem);
}

From source file:psidev.psi.mi.tab.client.gui.DragAndDropConverter.java

private static void addHelpMenu(final JFrame frame, JMenuBar menuBar) {

    // Create a menu
    JMenu menu = new JMenu("Help");
    menuBar.add(menu);

    // Create a file open iten
    JMenuItem item = new JMenuItem("About");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(frame, ABOUT_CONTENT, "About PSIMITAB Converter",
                    JOptionPane.INFORMATION_MESSAGE);
        }//  ww w  . j  a  va2s. co  m
    });
    menu.add(item);
}

From source file:psidev.psi.mi.validator.client.gui.DragAndDropValidator.java

/**
 * Sets up the menu bar of the main window.
 *
 * @param frame the frame onto which we attach the menu bar
 * @param table the table that allow to command the validator.
 * @param level the level of log message.
 *//*  w w w .  j  a v  a  2  s  . c  o m*/
public static void setupMenus(final JFrame frame, final Validator validator, final ValidatorTable table,
        final MessageLevel level) {

    // Create the fileMenu bar
    JMenuBar menuBar = new JMenuBar();

    final UserPreferences preferences = validator.getUserPreferences();

    ////////////////////////////
    // 1. Create a file menu
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    // Create a file Menu items
    JMenuItem openFile = new JMenuItem("Open file...");
    openFile.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            GuiUserPreferences guiPrefs = new GuiUserPreferences(preferences);

            File lastDirectory = userPreferences.getLastOpenedDirectory();

            JFileChooser fileChooser = new JFileChooser(lastDirectory);
            fileChooser.addChoosableFileFilter(new XmlFileFilter());

            // Open file dialog.
            fileChooser.showOpenDialog(frame);

            File selected = fileChooser.getSelectedFile();

            if (selected != null) {
                userPreferences.setLastOpenedDirectory(selected.getParentFile());

                // start validation of the selected file.
                ValidatorTableRow row = new ValidatorTableRow(selected, level, true);

                table.addTableRow(row);
            }
        }
    });
    fileMenu.add(openFile);

    // exit menu item
    JMenuItem exit = new JMenuItem("Exit");
    exit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            System.exit(0);
        }
    });
    fileMenu.add(exit);

    ///////////////////////////////
    // 2. TODO setup messages menu

    //        JMenu msgMenu = new JMenu( "Messages" );
    //        menuBar.add( msgMenu );
    //
    //        // Create a clear all messages item
    //        JMenuItem clearAll = new JMenuItem( "Clear all" );
    //        openFile.addActionListener( new ActionListener() {
    //
    //            public void actionPerformed( ActionEvent e ) {
    //
    //                // remove all elements
    //                table.removeAllRows( );
    //            }
    //        } );
    //        msgMenu.add( clearAll );

    //////////////////////////////
    // Log level

    // TODO create a menu that allows to switch log level at run time.

    /////////////////////////
    // 3. setup help menu
    JMenu helpMenu = new JMenu("Help");
    menuBar.add(helpMenu);

    JMenuItem about = new JMenuItem("About...");
    about.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JOptionPane msg = new JOptionPane("Version " + VERSION + NEW_LINE + "Authors: " + AUTHORS);
            JDialog dialog = msg.createDialog(frame, "About PSI Validator");
            dialog.setResizable(true);
            dialog.pack();
            dialog.setVisible(true);
        }
    });
    helpMenu.add(about);

    // Install the fileMenu bar in the frame
    frame.setJMenuBar(menuBar);
}

From source file:s3ml.DFA.DFA.java

public JFrame print(boolean visible) {
    DirectedSparseGraph<State, Transition> graph = new DirectedSparseGraph<>();

    for (State s : states) {
        graph.addVertex(s);/*w ww.j a  v a  2s.  c o  m*/
    }

    for (Transition t : transitions) {
        graph.addEdge(t, t.a, t.b, EdgeType.DIRECTED);
    }

    KKLayout<State, Transition> kkLayout = new KKLayout(graph);
    kkLayout.setSize(new Dimension(600, 600));

    final VisualizationViewer<State, Transition> vv = new VisualizationViewer<>(kkLayout);

    vv.getRenderContext().setVertexLabelTransformer(new Transformer<State, String>() {

        @Override
        public String transform(State i) {
            return i.name;
        }

    });
    vv.getRenderContext().setEdgeLabelTransformer(new Transformer<Transition, String>() {

        @Override
        public String transform(Transition i) {
            return "" + i.symbol;
        }

    });
    final Stroke stroke = new Stroke() {
        private Stroke stroke1, stroke2;

        {
            this.stroke1 = new BasicStroke(5f);
            this.stroke2 = new BasicStroke(1f);
        }

        @Override
        public Shape createStrokedShape(Shape shape) {
            return stroke2.createStrokedShape(stroke1.createStrokedShape(shape));
        }
    };
    vv.getRenderContext().setVertexStrokeTransformer(new Transformer<State, Stroke>() {
        @Override
        public Stroke transform(State i) {
            if (i.startState) {
                return new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
                        new float[] { 2.0f }, 0.0f);
            } else if (i.finalState) {
                return stroke;
            } else {
                return vv.getRenderContext().getGraphicsContext().getStroke();
            }
        }
    });

    vv.getRenderContext().setVertexFillPaintTransformer(new Transformer<State, Paint>() {
        @Override
        public Paint transform(State i) {
            if (i.finalState) {
                return Color.RED;
            } else if (i.startState) {
                return Color.CYAN;
            } else {
                return Color.LIGHT_GRAY;
            }
        }
    });

    DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
    gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
    vv.setGraphMouse(gm);
    final JFrame frame = new JFrame("V:" + graph.getVertexCount() + " E:" + graph.getEdgeCount());
    JMenuBar jMenuBar = new JMenuBar();
    jMenuBar.add(gm.getModeMenu());
    frame.setJMenuBar(jMenuBar);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(vv);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(visible);
    return frame;
}

From source file:Samples.Advanced.GraphEditorDemo.java

/**
 * a driver for this demo/*from www  . j a va  2 s . c  o m*/
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    JMenu matrixMenu = new JMenu();
    matrixMenu.setText("Matrix");
    matrixMenu.setIcon(null);
    matrixMenu.setPreferredSize(new Dimension(80, 20));
    menuBar.add(matrixMenu);
    JMenuItem copyMatrix = new JMenuItem("Copy Matrix to clipboard", KeyEvent.VK_C);
    copyMatrix.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    menuBar.add(copyMatrix);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    copyMatrix.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Vem textTransfer = new Vem();

            //Set up Matrix
            List<Integer[]> graphMatrix = new ArrayList<Integer[]>();
            int MatrixSize = graph.getVertexCount();

            Integer[] activeV = new Integer[MatrixSize];
            int count = 0;
            int activeVPos = 0;

            while (activeVPos < MatrixSize) {
                if (graph.containsVertex(count)) {
                    activeV[activeVPos] = count;
                    activeVPos++;
                }
                count++;
            }

            // sgv.g.getVertices().toArray()  ((Integer[])(sgv.g.getVertices().toArray()))
            for (int i = 0; i < MatrixSize; i++) {
                Integer[] tempArray = new Integer[MatrixSize];
                for (int j = 0; j < MatrixSize; j++) {
                    if (graph.findEdge(activeV[i], activeV[j]) != null) {
                        tempArray[j] = 1;
                    } else {
                        tempArray[j] = 0;
                    }
                }
                graphMatrix.add(tempArray);
            }
            //graphMatrix.add(new Integer[]{1, 2, 3});
            //graphMatrix.add(new Integer[]{4, 5 , 6, 7});

            //System.out.println(matrixToString(graphMatrix));
            //System.out.println(matrixToMathematica(graphMatrix));

            textTransfer.setClipboardContents("" + matrixToMathematica(graphMatrix));
            System.out.println("Clipboard contains:" + textTransfer.getClipboardContents());
        }
    });

    frame.pack();
    frame.setVisible(true);
}

From source file:Samples.Advanced.GraphEditorDemo2.java

/**
 * a driver for this demo/* w  w w .j a  v a  2 s  .c o m*/
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo2 demo = new GraphEditorDemo2();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:savant.plugin.builtin.SAFEBrowser.java

void initSafe(final String username, final String password)
        throws MalformedURLException, JDOMException, IOException {

    safeCard.removeAll();/* ww  w  .j av a  2s.  c  om*/
    safeCard.setLayout(new BorderLayout());

    File f = NetworkUtils.downloadFile(
            new URL(BrowserSettings.SAFE_URL + "?type=list&username=" + username + "&password=" + password),
            DirectorySettings.getTmpDirectory(), null);

    if (!wereCredentialsValid(f)) {
        DialogUtils.displayMessage("Login failed.");
        return;
    }

    final Component mainp = getCenterPanel(getDownloadTreeRows(f));
    safeCard.add(mainp, BorderLayout.CENTER);

    JMenuBar bottombar = new JMenuBar();
    bottombar.setAlignmentX(RIGHT_ALIGNMENT);
    bottombar.add(Box.createHorizontalGlue());

    /*
    JButton refbutt = new JButton("Refresh");
    refbutt.putClientProperty( "JButton.buttonType", "default" );
    refbutt.addActionListener(new ActionListener() {
            
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            System.out.println("Refreshing");
            safeCard.remove(mainp);
            File f = DownloadFile.downloadFile(new URL("http://savantbrowser.com/safe/savantsafe.php?username=" + username + "&password=" + password), System.getProperty("java.io.tmpdir"));
            Component newmainp = getCenterPanel(getDownloadTreeRows(f));
            safeCard.add(newmainp, BorderLayout.CENTER);
            container.invalidate();
            System.out.println("Done Refreshing");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    });
    bottombar.add(refbutt);
     *
     */

    JButton addgroupbutt = new JButton("Create group");
    addgroupbutt.putClientProperty("JButton.buttonType", "default");
    addgroupbutt.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                addGroup(username, password);
            } catch (Exception ex) {
                LOG.error("Unable to create group: " + ex.getLocalizedMessage());
            }
        }
    });
    bottombar.add(addgroupbutt);

    JButton logoutbutt = new JButton("Logout");
    logoutbutt.putClientProperty("JButton.buttonType", "default");
    logoutbutt.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            layout.show(container, "login");
        }
    });
    bottombar.add(logoutbutt);

    JButton openbutt = new JButton("Load Track");
    openbutt.putClientProperty("JButton.buttonType", "default");
    openbutt.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            actOnSelectedItem(false);
        }
    });
    bottombar.add(openbutt);

    safeCard.add(bottombar, BorderLayout.SOUTH);

    layout.show(container, "safe");
}