Java Swing Tutorial - Java JMenu.insert(Action a, int pos)








Syntax

JMenu.insert(Action a, int pos) has the following syntax.

public JMenuItem insert(Action a,  int pos)

Example

In the following code shows how to use JMenu.insert(Action a, int pos) method.

import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
//  w w  w . jav a 2s  .  c o  m
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

public class Main extends JFrame {
  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);
    add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
    
    menu.insert(new AbstractAction("Action"){

      @Override
      public void actionPerformed(ActionEvent arg0) {

        
      }
      
    },0);
  }

  public static void main(String arg[]) {
    new Main();
  }
}