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();
}
}
No comments:
Post a Comment