Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By camickr

Thread: MenuBar, menus & actions

  1. #1
    Agrata is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Lightbulb MenuBar, menus & actions

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;



    public class MenuBarDemo extends JFrame implements ActionListener ,ItemListener {

    public static void main(String[] args) {
    new MenuBarDemo();
    }

    JRadioButtonMenuItem radio;
    JCheckBoxMenuItem ch;

    MenuBarDemo() {

    addWindowListener(new WindowAdapter() {
    public void WindowClosing(WindowEvent e) {
    System.exit(0);
    }});

    JMenuBar mb = new JMenuBar();
    JMenu m = new JMenu("Tick");
    JMenuItem m1 = new JMenuItem("Exit");
    m1.setActionCommand("Exit");
    m1.addActionListener(this);
    radio = new JRadioButtonMenuItem("File chooser");
    radio.addItemListener(this);
    ch = new JCheckBoxMenuItem("JTable");
    ch.addItemLIstener(this);
    m.add(m1);
    m.add(radio);
    m.add(ch);
    mb.add(m);
    mb.setVisible(true);
    setSize(400,400);
    setResizable(true);
    setLayout(new FlowLayout());
    setJMenuBar(mb);
    setVisible(true);

    }

    public void actionPerformed(ActionEvent ae) {
    String s = ae.getActionCommand();

    if(s.equals("Exit"))
    System.exit(0);

    }

    public void itemStateChanged(ItemEvent ie) {

    if(radio.isSelected()==true)
    JFrame f = new JFrame("File chooser");
    JFileChooser fc = new JFileChooser();
    fc.setVisible(true);
    f.add(fc);
    f.setVisible(true);
    f.setSize(400,400);
    f.setResizable(false);
    }

    if(ch.isSelected()==true) {
    JFrame f1 = new JFrame("JTable");
    JTable t = new JTable(20,10);

    f1.add(t);
    t.setVisible(true);
    f1.setSize(600,600);
    f1.setResizable(false);
    f1.setVisible(true);

    }
    }
    }


    MenuBar, menus & actions-menubaroutput.jpg

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: MenuBar, menus & actions

    1) Use the "code tags" so your code retains its formatting.
    2) Is there a question?
    3) Read the Swing tutorial for a working example: How to Use Menus (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Agrata likes this.

  3. #3
    Agrata is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Exclamation Re: MenuBar, menus & actions

    Many thanks for your reference.I am enjoying learning the Java TM tutorial.The improved
    version of menubar,menus & actions have been posting quickly.please check & again give
    reference if you could.
    Agrata.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. runtime MenuBar Generator
    By vishrut_n_shah in forum AWT / Swing
    Replies: 5
    Last Post: 10-31-2011, 07:46 AM
  2. Vertical menubar
    By Java Unknown in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 05:50 AM
  3. About MenuBar
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 03-20-2009, 11:50 AM
  4. Help about Java Menubar
    By makpandian in forum AWT / Swing
    Replies: 5
    Last Post: 02-09-2009, 08:17 AM
  5. menubar in splitpane
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 12-16-2008, 09:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •