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:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    ComponentOrientation ori = ComponentOrientation.LEFT_TO_RIGHT;

    menu.applyComponentOrientation(ori);
    bar.add(menu);

    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"));

    setJMenuBar(bar);//from w  w  w . j av a 2s  . c  om
    add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);

    menu.insert(new AbstractAction("Action") {

        @Override
        public void actionPerformed(ActionEvent arg0) {

        }

    }, 0);
}

From source file:DialogTest.java

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

    // construct a File menu

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*www . j  a  v a2 s  . c o m*/
    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:ImageIOTest.java

public ImageIOFrame() {
    setTitle("ImageIOTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            openFile();//from   w  w  w .  j  a  va  2 s. c  om
        }
    });
    fileMenu.add(openItem);

    JMenu saveMenu = new JMenu("Save");
    fileMenu.add(saveMenu);
    Iterator<String> iter = writerFormats.iterator();
    while (iter.hasNext()) {
        final String formatName = iter.next();
        JMenuItem formatItem = new JMenuItem(formatName);
        saveMenu.add(formatItem);
        formatItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                saveFile(formatName);
            }
        });
    }

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

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);
}

From source file:ProgressMonitorInputStreamTest.java

public TextFrame() {
    setTitle("ProgressMonitorInputStreamTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    textArea = new JTextArea();
    add(new JScrollPane(textArea));

    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*from   w w  w. ja v a2s.  c o  m*/
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    openItem = new JMenuItem("Open");
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                openFile();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    });

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

From source file:com.google.code.facebook.graph.sna.applet.GraphFromGraphMLDemo.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features./*from  w  ww .j av a 2s .  c  o  m*/
 * @throws SAXException 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * 
 */
public GraphFromGraphMLDemo(String filename) throws ParserConfigurationException, SAXException, IOException {

    Factory<Number> vertexFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };

    GraphMLReader<DirectedGraph<Number, Number>, Number, Number> gmlr = new GraphMLReader<DirectedGraph<Number, Number>, Number, Number>(
            vertexFactory, edgeFactory);
    final DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    gmlr.load(filename, graph);

    // create a simple graph for the demo
    vv = new VisualizationViewer<Number, Number>(new FRLayout<Number, Number>(graph));

    vv.addGraphMouseListener(new TestGraphMouseListener<Number>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<Number, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Number, Number>();
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    JMenuBar menubar = new JMenuBar();
    menubar.add(graphMouse.getModeMenu());
    panel.setCorner(menubar);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    content.add(controls, BorderLayout.SOUTH);

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

From source file:edu.uci.ics.jung.samples.GraphFromGraphMLDemo.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features./* w w w .ja v  a 2 s . co m*/
 * @throws SAXException 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * 
 */
public GraphFromGraphMLDemo(String filename) throws ParserConfigurationException, SAXException, IOException {

    Factory<Number> vertexFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };

    GraphMLReader<Graph<Number, Number>, Number, Number> gmlr = new GraphMLReader<Graph<Number, Number>, Number, Number>(
            vertexFactory, edgeFactory);
    //final DirectedGraph<Number,Number> graph = new DirectedSparseMultigraph<Number,Number>();
    final Graph<Number, Number> graph = new SparseGraph<Number, Number>();
    gmlr.load(filename, graph);

    // create a simple graph for the demo
    vv = new VisualizationViewer<Number, Number>(new FRLayout<Number, Number>(graph));

    vv.addGraphMouseListener(new TestGraphMouseListener<Number>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<Number, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Number, Number>();
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    JMenuBar menubar = new JMenuBar();
    menubar.add(graphMouse.getModeMenu());
    panel.setCorner(menubar);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    content.add(controls, BorderLayout.SOUTH);

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

From source file:eu.fbk.sonet.snatool.GraphVisualizer.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features./*from   w ww  . j a va 2  s .c  o  m*/
 * @throws SAXException 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * 
 */

public GraphVisualizer(String filename) throws ParserConfigurationException, SAXException, IOException {

    Factory<Number> vertexFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {
        int n = 0;

        public Number create() {
            return n++;
        }
    };

    GraphMLReader<DirectedGraph<Number, Number>, Number, Number> gmlr = new GraphMLReader<DirectedGraph<Number, Number>, Number, Number>(
            vertexFactory, edgeFactory);
    final DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    gmlr.load(filename, graph);

    // create a simple graph for the demo
    vv = new VisualizationViewer<Number, Number>(new FRLayout<Number, Number>(graph));

    //vv.addGraphMouseListener(new TestGraphMouseListener<Number>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<Number, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Number, Number>();
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    JMenuBar menubar = new JMenuBar();
    menubar.add(graphMouse.getModeMenu());
    panel.setCorner(menubar);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    // Scaling graph image        
    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    // Adding control to controls JPanel
    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    content.add(controls, BorderLayout.SOUTH);

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

From source file:io.datalayer.jung.SdpGraphMLDemo.java

/**
 * create an instance of a simple graph with controls to demo the zoom
 * features.// w  w  w  .j av  a 2 s  . co  m
 * 
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws IOException
 */
public SdpGraphMLDemo(String filename) throws ParserConfigurationException, SAXException, IOException {

    Factory<Number> vertexFactory = new Factory<Number>() {

        int n = 0;

        public Number create() {

            return n++;
        }
    };
    Factory<Number> edgeFactory = new Factory<Number>() {

        int n = 0;

        public Number create() {

            return n++;
        }
    };

    GraphMLReader<DirectedGraph<Number, Number>, Number, Number> gmlr = new GraphMLReader<DirectedGraph<Number, Number>, Number, Number>(
            vertexFactory, edgeFactory);
    final DirectedGraph<Number, Number> graph = new DirectedSparseMultigraph<Number, Number>();
    gmlr.load(filename, graph);

    // create a simple graph for the demo
    vv = new VisualizationViewer<Number, Number>(new FRLayout<Number, Number>(graph));

    vv.addGraphMouseListener(new TestGraphMouseListener<Number>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<Number, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<Number>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {

        public String transform(Number edge) {

            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Number>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Number, Number>();
    vv.setGraphMouse(graphMouse);
    vv.addKeyListener(graphMouse.getModeKeyListener());

    JMenuBar menubar = new JMenuBar();
    menubar.add(graphMouse.getModeMenu());
    panel.setCorner(menubar);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    content.add(controls, BorderLayout.SOUTH);

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

From source file:components.InternalFrameDemo.java

protected JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();

    //Set up the lone menu.
    JMenu menu = new JMenu("Document");
    menu.setMnemonic(KeyEvent.VK_D);
    menuBar.add(menu);

    //Set up the first menu item.
    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("new");
    menuItem.addActionListener(this);
    menu.add(menuItem);/*  www .  jav  a 2  s .com*/

    //Set up the second menu item.
    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("quit");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    return menuBar;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu(new AbstractAction("File") {

        @Override/*from ww  w.j  ava 2s.  com*/
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("action");

        }

    });
    bar.add(menu);

    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new AbstractAction("Exit") {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("action");

        }

    });

    setJMenuBar(bar);
    add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}