Skip to main content

JMenu Example in Swing for Beginners

The following example illustrates JMenu and JMenuItem in swing. This example covers all important methods and constructors of JMenu class.

import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
class JMenuDemo extends JFrame
{
JMenuBar mb;
JMenu m1,m2,m3,m4;
JMenu submenu;
JMenuItem i1,i2,i3,i4,i5,i6,i7,i8,i9,i10;

    public JMenuDemo()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JMenu Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
       
        mb=new JMenuBar();
       
        m1=new JMenu();
        m2=new JMenu(new DefaultEditorKit.BeepAction());
        m3=new JMenu("Edit");
        m4=new JMenu("Format",true);
       
        // Create sub menu
        submenu=new JMenu("Sub menu");
       
        // Set delay
        // Menu items in submenu get displayed
        // after 1000ms
        // This method doesn't work for top-level
        // menus in most cases, you need to write
        // extra code to do that
        submenu.setDelay(1000);
       
        // Set menu location to down
        int x=submenu.getLocation().x;
        int y=submenu.getLocation().y;
        y=y+submenu.getPreferredSize().height;
        submenu.setMenuLocation(x,y);

        // Set some text
        m1.setText("File");
       
        i1=new JMenuItem("New");
        i2=new JMenuItem("Open");
        i3=new JMenuItem("Save");
        i4=new JMenuItem("Save As");
        i5=new JMenuItem("Exit");
       
        i6=new JMenuItem("Cut");
        i7=new JMenuItem("Copy");
        i8=new JMenuItem("Paste");
       
        i9=new JMenuItem("Wordwrap");
        i10=new JMenuItem("Font");
       
        // Add some items
        submenu.add(new JMenuItem("Item 1"));
        submenu.add(new JMenuItem("Item 2"));
        submenu.add(new JMenuItem("Item 3"));
        submenu.add(new JMenuItem("Item 4"));
        submenu.add(new JMenuItem("Item 5"));
       
        // Add items
        m1.add(new JButton("This is a button"));
       
        m1.add(i1);
        m1.add(i2);
        m1.add(i3);
        m1.add(i4);
       
        // add(String) creates JMenuItem
        // with given text and adds it
        m1.add("Print");
       
        m1.add(i5);
       
        // Add separator
        m1.addSeparator();
       
        m1.add(submenu);
       
        m3.add(i6);
        m3.add(i7);
        m3.add(i8);
       
        m4.add(i9);
        m4.add(i10);
       
        // Inserts jmenuitem with given text
        m4.insert("Item",0);
       
        // Alternative way for above
        // This returns JMenuItem with given text
        // m4.insert(new JMenuItem("Item"),0);
       
        mb.add(m1);
        mb.add(m2);
        mb.add(m3);
        mb.add(m4);
       
        setJMenuBar(mb);
       
       
       
        // includes button,separator,submenu,menu items (everything)
        System.out.println("Item count for m1 "+m1.getItemCount());
       
        setSize(400,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        new JMenuDemo();
    }
}

Screenshot of JMenu Example

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Creating Transparent JPanel in Swing

It is a quite common question, "how to create transparent panels in swing" and if you think that the solution is critical and involves a lot of hacks, you are wrong. Yes, because creating transparent panels in swing is as easy and you are already might be familiar with it.