Menus in Swing

How do you respond to menu events in Swing?

you add an action listener to the menu ... look up the swing help thread or the doc for ActionListener

Similar Messages

  • Animated menus in Swing?

    Has anyone tried doing or seen examples of animated menus and buttons made in java (swing)
    What im looking for, is input on how to create menues that i.e. opens by scrolling down or fading up the popupmenu.
    Looked at http://search.java.sun.com/ClickThru?qt=slides+menu&url=http%3A%2F%2Fforum.java.sun.com%2Fthread.jsp%3Fforum%3D57%26thread%3D277596&pathInfo=%2Fsearch%2Fjava%2Findex.jsp&hitNum=2&col=javaforums
    which did'nt help much...
    Thanx in advance...
    Barsum

    One simple way is:
    label.setText("<html><img src=.....></html>");

  • How to tell if your component is being overlapped by another

    Hi,
    I've run into a problem implementing a menu bar into my program. Here's a little graphic. :)
              |  FILE     EDIT                                                              |
              |                                                                                            |
              |                                                                                            |
              |                                                                                            |
              |     Some custom component         CustomPanel     |
              |                                                                                            |
              |                                                                                            |
              |                                                                                            |
              ----------------------------------------------------------------------Except my component uses an active rendering loop, so it covers over the flip down menu. Is there anyway to tell when something is covering the CustomPanel? Or is there anyway to explicitly tell a component to paint all overlapping components on it?
    such as: CustomPanel.paintOverlappingComponents().
    or something like that?

    Menus in swing are light weight components i believe.
    if you want them to appear over the top of an actively rendered Component, you are going to need to either :-
    1) forcefully repaint the menus as part of the active rendering. (so, repainting them each loop)
    2) when performing the active rendering, add a clip region that excludes the area that the menus take up. (avoid overpainting them at all)
    In either case, you are going to have to query the menus each render loop, to determine if they are showing or not.
    I would go for solution 2, it should be quicker.

  • JAWS does not always read  Swing menus

    I have come across a problem that involves JAWS 4.51 and Java menus in Internet Explorer.
    I have a Java application that has a JMenuBar. The menu bar contains several JMenus that in turn contain several JMenuItems.
    When navigating through a menu, JAWS reads every JMenuItem. However, once a disabled JMenuItem is reached, JAWS no longer reads the JMenuItems below the disabled one in the open JMenu.
    The problem occurs only in Internet Explorer on "plain" menu items that follow a disabled menu item. The problem does not affect "complex" menu items, such as those that open sub menus.
    I can only reproduce this problem in Internet Explorer 6. Netscape Communicator 7, Web Start clients and a command line launch do not suffer from this problem. Also, JAWS 4.02 is fine as well; only 4.51 has this problem.
    Attached at the end is a sample application that recreates the issue. You'll notice in the sample application that JAWS will not read "A check box menu item" and "Another one" while it will read "A submenu."
    Does anyone have a solution to this? Thanks a lot!
    Sample application:
    ======================================================
    Note: The code below is a modification of the Sun menu tutorial found at http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
    middle.gif can be downloaded from http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/images/middle.gif
    MenuLookDemoApplet.java:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    import javax.swing.JMenuBar;
    import javax.swing.KeyStroke;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JApplet;
    /* MenuLookDemoApplet.java is a 1.4 application that requires images/middle.gif. */
    * This class exists solely to show you what menus look like.
    * It has no menu-related event handling.
    public class MenuLookDemoApplet extends JApplet {
        JTextArea output;
        JScrollPane scrollPane;
        public JMenuBar createMenuBar() {
            JMenuBar menuBar;
            JMenu menu, submenu;
            JMenuItem menuItem;
            JRadioButtonMenuItem rbMenuItem;
            JCheckBoxMenuItem cbMenuItem;
            //Create the menu bar.
            menuBar = new JMenuBar();
            //Build the first menu.
            menu = new JMenu("A Menu");
            menu.setMnemonic(KeyEvent.VK_A);
            menu.getAccessibleContext().setAccessibleDescription(
                    "The only menu in this program that has menu items");
            menuBar.add(menu);
            //a group of JMenuItems
            menuItem = new JMenuItem("A text-only menu item",
                                     KeyEvent.VK_T);
            //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
            menuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_1, ActionEvent.ALT_MASK));
            menuItem.getAccessibleContext().setAccessibleDescription(
                    "This doesn't really do anything");
            menu.add(menuItem);
            ImageIcon icon = createImageIcon("images/middle.gif");
            menuItem = new JMenuItem("Both text and icon", icon);
            menuItem.setMnemonic(KeyEvent.VK_B);
            menu.add(menuItem);
            menuItem = new JMenuItem(icon);
            menuItem.setMnemonic(KeyEvent.VK_D);
            menu.add(menuItem);
            //a group of radio button menu items
            menu.addSeparator();
            ButtonGroup group = new ButtonGroup();
            rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
            rbMenuItem.setSelected(true);
            rbMenuItem.setMnemonic(KeyEvent.VK_R);
            group.add(rbMenuItem);
            menu.add(rbMenuItem);
            rbMenuItem = new JRadioButtonMenuItem("Another one");
            rbMenuItem.setMnemonic(KeyEvent.VK_O);
            group.add(rbMenuItem);
            menu.add(rbMenuItem);
            rbMenuItem.setEnabled(false);   //@dpb
            //a group of check box menu items
            menu.addSeparator();
            cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
            cbMenuItem.setMnemonic(KeyEvent.VK_C);
            menu.add(cbMenuItem);
            cbMenuItem = new JCheckBoxMenuItem("Another one");
            cbMenuItem.setMnemonic(KeyEvent.VK_H);
            menu.add(cbMenuItem);
            //a submenu
            menu.addSeparator();
            submenu = new JMenu("A submenu");
            submenu.setMnemonic(KeyEvent.VK_S);
            menuItem = new JMenuItem("An item in the submenu");
            menuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_2, ActionEvent.ALT_MASK));
            submenu.add(menuItem);
            menuItem = new JMenuItem("Another item");
            submenu.add(menuItem);
            menu.add(submenu);
            //Build second menu in the menu bar.
            menu = new JMenu("Another Menu");
            menu.setMnemonic(KeyEvent.VK_N);
            menu.getAccessibleContext().setAccessibleDescription(
                    "This menu does nothing");
            menuBar.add(menu);
            return menuBar;
        public Container createContentPane() {
            //Create the content-pane-to-be.
            JPanel contentPane = new JPanel(new BorderLayout());
            contentPane.setOpaque(true);
            //Create a scrolled text area.
            output = new JTextArea(5, 30);
            output.setEditable(false);
            scrollPane = new JScrollPane(output);
            //Add the text area to the content pane.
            contentPane.add(scrollPane, BorderLayout.CENTER);
            return contentPane;
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = MenuLookDemo.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
        public void init() {
            MenuLookDemoApplet demo = new MenuLookDemoApplet();
            setJMenuBar(demo.createMenuBar());
            setContentPane(demo.createContentPane());
    applet.html:
    <html>
    <head>
        <title>Menu Test Page </title>
    </head>
    <body>
    Here's the applet:
    <applet code="MenuLookDemoApplet.class" width="300" height="300">
    </applet>
    </body>
    </html>

    Unfortunately the menu items are fully accessible (or so our internal accessibility tool claims). It doesn't seem to be a problem on that level, but I was hoping that perhaps something else was interfeering.
    As for the JTable reading the previous cell, I've notice that it reads the last non-empty cell (regardless of whether it was the previous cell or not) when clicking or moving with the arrow keys to an empty cell. It works fine when clicking or moving to non-empty cells.
    I don't have any empty cells in the actual application so I did not try to fix it. My suspicions are that either an event is not being fired or the selection position is not being updated on the empty cells.

  • Do swing components work in sub-menus of popup menus??

    I want to have a JSlider as a component of a sub-menu of a pop-up menu. it shows up but acts very strangely. If I grab the slider handle and drag, nothing happens. If I click to one side of the handle, the slider makes single steps repeatedly until it catches up with the position i click on.
    On the other hand, if I add a JSlider to a top-level pop-up menu, it works just fine.
    are swing components supposed to work in sub-menus of popup menus??
    i'm running java 1.5 on Windows XP.
    Thanks, - Conal

    The music store is part of iTunes. That is why they are together on the menu. As many topics would offer the same advice for music in your library or music in the store, grouping them together makes sense in terms of reducing redundant entries.
    Sorry you found it confusing. I, too, once found some things Apple a bit different. That was about two years ago when I bought my iBook. StarDeb is another recent switcher. And I feel confident in saying this: either of us would be more than happy to help you with anything that gets "lost in translation". We both know there is a learning curve when you move from the XP world to a more fruit-based view of things

  • Complie Error for menus (Swing)

    I'm receiving the following complie error. Also, listed is the area for code that is getting the error.
    Thank you for any help that you can provide.
    Error Message:
    P5AWT should be declared abstract; it does not define menuSelected(javax.swing.event.MenuEvent) in P5AWT
    public class P5AWT extends JFrame
    Program:
    public class P5AWT extends JFrame
         implements ActionListener, MenuListener
    private JTextField inputfile;
    private JTextField outputfile;
    private JTextArea ta;
    private JMenuItem newMT, openMT, saveAsMT, exitMT;
    String curFile, filename = "";
    int numstaff;
    Employee[] staff;
    public P5AWT()
    super ("P5AWT Starter Program");
    addWindowListener(new WindowAdapter()
    { public void windowClosing(WindowEvent e) { System.exit(0); }} );
    JMenuBar mb = new JMenuBar ();
    setMenuBar(mb);
    JMenu fileMenu = new JMenu ("File");
    mb.add(fileMenu);
    fileMenu.add (newMT = new JMenuItem ("New", new MenuShortcut(KeyEvent.VK_N)));
    newMT.addMenuListener( new NewCommand());
    fileMenu.add (openMT = new JMenuItem ("Open", new MenuShortcut(KeyEvent.VK_O)));
    openMT.addMenuListener( new LoadFileCommand());
    fileMenu.add (saveAsMT = new JMenuItem ("Save As"));
    saveAsMT.addMenuListener( new SaveAsFileCommand());
    fileMenu.add (exitMT = new JMenuItem ("Exit"));
    exitMT.addMenuListener( new CloseCommand());
    JPanel p3 = new JPanel();
    p3.setLayout(new FlowLayout(FlowLayout.CENTER));
    ta = new JTextArea(20, 60);
    p3.add(ta);
    setLayout(new FlowLayout(FlowLayout.CENTER)); // this overides everything, why?
    add(p3);
    }

    menuSelected is a Abstract method. That means if you implement the MenuListener Class you SHOULD define the menuSelected() event.
    I am not correct with the Method names but logically it seems that tis is the problem.
    Another thing, use setJMenuBar() instead setMenuBar()
    Regards ,
    Karan

  • Javascript or applet dropdown menus won't work in IE6, How can I solve this

    I've made some dropdownmenu's of javascript and one of java applets. But they don't work in IE6. If IE6 is installed they won't work either. My friend has IE5.5 and he has no problems with the menus. How can you make them work.
    And how can you change the z-index of a javascipt/applet? How do you make a dropdownmenu that's put in the topframe slide over the mainframe?

    Perhaps someone could help me with the following question:
    I have a menu that cascades out of the Applet box. My question is:
    How do I close the menu without clicking on the Applet box? - i.e. if the user clicks on the HTML area of the WEB-PAGE, or simply waits a second or 2, the menu should collapse?
    I notice that this is actually the default behavior for the AWT classes - but then you can't set background colors etc. I need the same kind of behavior, but using Swing classes.
    Would appreciate any assistance or advice...

  • How to write a code for  open new txt file in swing

    hai all,
    now i do one project in java.that project's GUI is Swing. But i don't known swing (basic).So how to write a code for open new txt file and "Open window " in menu item on swing.that means when i click the "New" on menu that time open a new txt file. open also like that type.
    plz give me that code ! very urgent
    Advance Thanks !
    RSK

    Swing Tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/index.html
    Since you don't know the basic of swing read the tutorial, it is for your own good because it is useless if we provide you with a code you don't even understand and how it works.
    If you want a menu read the tutorial about using menus and for opening a file read using JFileChooser.
    note: don't use the word urgent because it implies that your problem is more important than others.

  • Why do the menus diseappear? please help.....

    I am quite new to Java and I use project builder on my mac running OSX. Anyway I found some sample code and I was wondering if anybody could help me out. Basically when the app is compiled it draws "Hello World" in the window and there are two menus in the menubar -> 'File' and 'Edit', but when you go to the about box the menus disappear until you either close the aboutbox or bring the focus back to the main window.
    How can I always keep the menus showing when there are multiple windows in the app. Remember that mac menus are fixed to the top of the screen...
    anyway heres the code:
    import java.awt.*;
    import java.awt.event.*;
    import com.apple.mrj.*;
    import javax.swing.*;
    public class test extends JFrame
                          implements  ActionListener,
                                      MRJAboutHandler,
                                      MRJQuitHandler
        static final String message = "Hello World!";
        private Font font = new Font("serif", Font.ITALIC+Font.BOLD, 36);
        protected AboutBox aboutBox;
        // Declarations for menus
        static final JMenuBar mainMenuBar = new JMenuBar();
        static final JMenu fileMenu = new JMenu("File");
        protected JMenuItem miNew;
        protected JMenuItem miOpen;
        protected JMenuItem miClose;
        protected JMenuItem miSave;
        protected JMenuItem miSaveAs;
        static final JMenu editMenu = new JMenu("Edit");
        protected JMenuItem miUndo;
        protected JMenuItem miCut;
        protected JMenuItem miCopy;
        protected JMenuItem miPaste;
        protected JMenuItem miClear;
        protected JMenuItem miSelectAll;
        public void addFileMenuItems() {
            miNew = new JMenuItem ("New");
            miNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.META_MASK));
            fileMenu.add(miNew).setEnabled(true);
            miNew.addActionListener(this);
            miOpen = new JMenuItem ("Open...");
            miOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.META_MASK));
            fileMenu.add(miOpen).setEnabled(true);
            miOpen.addActionListener(this);
            miClose = new JMenuItem ("Close");
            miClose.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.Event.META_MASK));
            fileMenu.add(miClose).setEnabled(true);
            miClose.addActionListener(this);
            miSave = new JMenuItem ("Save");
            miSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.META_MASK));
            fileMenu.add(miSave).setEnabled(true);
            miSave.addActionListener(this);
            miSaveAs = new JMenuItem ("Save As...");
            fileMenu.add(miSaveAs).setEnabled(true);
            miSaveAs.addActionListener(this);
            mainMenuBar.add(fileMenu);
        public void addEditMenuItems() {
            miUndo = new JMenuItem("Undo");
            miUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.Event.META_MASK));
            editMenu.add(miUndo).setEnabled(true);
            miUndo.addActionListener(this);
            editMenu.addSeparator();
            miCut = new JMenuItem("Cut");
            miCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.META_MASK));
            editMenu.add(miCut).setEnabled(true);
            miCut.addActionListener(this);
            miCopy = new JMenuItem("Copy");
            miCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.META_MASK));
            editMenu.add(miCopy).setEnabled(true);
            miCopy.addActionListener(this);
            miPaste = new JMenuItem("Paste");
            miPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.META_MASK));
            editMenu.add(miPaste).setEnabled(true);
            miPaste.addActionListener(this);
            miClear = new JMenuItem("Clear");
            editMenu.add(miClear).setEnabled(true);
            miClear.addActionListener(this);
            editMenu.addSeparator();
            miSelectAll = new JMenuItem("Select All");
            miSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.Event.META_MASK));
            editMenu.add(miSelectAll).setEnabled(true);
            miSelectAll.addActionListener(this);
            mainMenuBar.add(editMenu);
        public void addMenus() {
            addFileMenuItems();
            addEditMenuItems();
            setJMenuBar (mainMenuBar);
        public test() {
            super("test");
            this.getContentPane().setLayout(null);
            addMenus();
            aboutBox = new AboutBox();
            Toolkit.getDefaultToolkit();
            MRJApplicationUtils.registerAboutHandler(this);
            MRJApplicationUtils.registerQuitHandler(this);
            setVisible(true);
        public void paint(Graphics g) {
                    super.paint(g);
            g.setColor(Color.blue);
            g.setFont (font);
            g.drawString(message, 40, 80);
        public void handleAbout() {
            aboutBox.setResizable(false);
            aboutBox.setVisible(true);
            aboutBox.show();
        public void handleQuit() {
            System.exit(0);
        // ActionListener interface (for menus)
        public void actionPerformed(ActionEvent newEvent) {
            if (newEvent.getActionCommand().equals(miNew.getActionCommand())) doNew();
            else if (newEvent.getActionCommand().equals(miOpen.getActionCommand())) doOpen();
            else if (newEvent.getActionCommand().equals(miClose.getActionCommand())) doClose();
            else if (newEvent.getActionCommand().equals(miSave.getActionCommand())) doSave();
            else if (newEvent.getActionCommand().equals(miSaveAs.getActionCommand())) doSaveAs();
            else if (newEvent.getActionCommand().equals(miUndo.getActionCommand())) doUndo();
            else if (newEvent.getActionCommand().equals(miCut.getActionCommand())) doCut();
            else if (newEvent.getActionCommand().equals(miCopy.getActionCommand())) doCopy();
            else if (newEvent.getActionCommand().equals(miPaste.getActionCommand())) doPaste();
            else if (newEvent.getActionCommand().equals(miClear.getActionCommand())) doClear();
            else if (newEvent.getActionCommand().equals(miSelectAll.getActionCommand())) doSelectAll();
        public void doNew() {}
        public void doOpen() {}
        public void doClose() {}
        public void doSave() {}
        public void doSaveAs() {}
        public void doUndo() {}
        public void doCut() {}
        public void doCopy() {}
        public void doPaste() {}
        public void doClear() {}
        public void doSelectAll() {}
        public static void main(String args[]) {
            new test();
    }������������������������������������������
    and the coe of the aboutbox
    ������������������������������������������
    //      File:   AboutBox.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class AboutBox extends JFrame
                          implements ActionListener
        protected JButton okButton;
        protected JLabel aboutText;
        public AboutBox() {
            super();
            this.getContentPane().setLayout(new BorderLayout(15, 15));
            this.setFont(new Font ("SansSerif", Font.BOLD, 14));
            aboutText = new JLabel ("About test");
            JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            textPanel.add(aboutText);
            this.getContentPane().add (textPanel, BorderLayout.NORTH);
            okButton = new JButton("OK");
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            buttonPanel.add (okButton);
            okButton.addActionListener(this);
            this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            this.pack();
        public void actionPerformed(ActionEvent newEvent) {
            setVisible(false);
    }thanks

    Why not make the about box a dialog in the frame which initially has setVisible(false) and only appears when required by chaning this to true, then back to false on closing.
    That way it shouldn't interfere with the menus, as it is always there, just not always visible...

  • Why do the menus disappear?

    I am quite new to Java and I use project builder on my mac running OSX. Anyway I found some sample code and I was wondering if anybody could help me out. Basically when the app is compiled it draws "Hello World" in the window and there are two menus in the menubar -> 'File' and 'Edit', but when you go to the about box the menus disappear until you either close the aboutbox or bring the focus back to the main window.
    How can I always keep the menus showing when there are multiple windows in the app. Remember that mac menus are fixed to the top of the screen...
    anyway heres the code:
    import java.awt.*;
    import java.awt.event.*;
    import com.apple.mrj.*;
    import javax.swing.*;
    public class test extends JFrame
                          implements  ActionListener,
                                      MRJAboutHandler,
                                      MRJQuitHandler
        static final String message = "Hello World!";
        private Font font = new Font("serif", Font.ITALIC+Font.BOLD, 36);
        protected AboutBox aboutBox;
        // Declarations for menus
        static final JMenuBar mainMenuBar = new JMenuBar();
        static final JMenu fileMenu = new JMenu("File");
        protected JMenuItem miNew;
        protected JMenuItem miOpen;
        protected JMenuItem miClose;
        protected JMenuItem miSave;
        protected JMenuItem miSaveAs;
        static final JMenu editMenu = new JMenu("Edit");
        protected JMenuItem miUndo;
        protected JMenuItem miCut;
        protected JMenuItem miCopy;
        protected JMenuItem miPaste;
        protected JMenuItem miClear;
        protected JMenuItem miSelectAll;
        public void addFileMenuItems() {
            miNew = new JMenuItem ("New");
            miNew.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.Event.META_MASK));
            fileMenu.add(miNew).setEnabled(true);
            miNew.addActionListener(this);
            miOpen = new JMenuItem ("Open...");
            miOpen.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.Event.META_MASK));
            fileMenu.add(miOpen).setEnabled(true);
            miOpen.addActionListener(this);
            miClose = new JMenuItem ("Close");
            miClose.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.Event.META_MASK));
            fileMenu.add(miClose).setEnabled(true);
            miClose.addActionListener(this);
            miSave = new JMenuItem ("Save");
            miSave.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.META_MASK));
            fileMenu.add(miSave).setEnabled(true);
            miSave.addActionListener(this);
            miSaveAs = new JMenuItem ("Save As...");
            fileMenu.add(miSaveAs).setEnabled(true);
            miSaveAs.addActionListener(this);
            mainMenuBar.add(fileMenu);
        public void addEditMenuItems() {
            miUndo = new JMenuItem("Undo");
            miUndo.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.Event.META_MASK));
            editMenu.add(miUndo).setEnabled(true);
            miUndo.addActionListener(this);
            editMenu.addSeparator();
            miCut = new JMenuItem("Cut");
            miCut.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.META_MASK));
            editMenu.add(miCut).setEnabled(true);
            miCut.addActionListener(this);
            miCopy = new JMenuItem("Copy");
            miCopy.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.META_MASK));
            editMenu.add(miCopy).setEnabled(true);
            miCopy.addActionListener(this);
            miPaste = new JMenuItem("Paste");
            miPaste.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.META_MASK));
            editMenu.add(miPaste).setEnabled(true);
            miPaste.addActionListener(this);
            miClear = new JMenuItem("Clear");
            editMenu.add(miClear).setEnabled(true);
            miClear.addActionListener(this);
            editMenu.addSeparator();
            miSelectAll = new JMenuItem("Select All");
            miSelectAll.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.Event.META_MASK));
            editMenu.add(miSelectAll).setEnabled(true);
            miSelectAll.addActionListener(this);
            mainMenuBar.add(editMenu);
        public void addMenus() {
            addFileMenuItems();
            addEditMenuItems();
            setJMenuBar (mainMenuBar);
        public test() {
            super("test");
            this.getContentPane().setLayout(null);
            addMenus();
            aboutBox = new AboutBox();
            Toolkit.getDefaultToolkit();
            MRJApplicationUtils.registerAboutHandler(this);
            MRJApplicationUtils.registerQuitHandler(this);
            setVisible(true);
        public void paint(Graphics g) {
              super.paint(g);
            g.setColor(Color.blue);
            g.setFont (font);
            g.drawString(message, 40, 80);
        public void handleAbout() {
            aboutBox.setResizable(false);
            aboutBox.setVisible(true);
            aboutBox.show();
        public void handleQuit() {     
            System.exit(0);
        // ActionListener interface (for menus)
        public void actionPerformed(ActionEvent newEvent) {
            if (newEvent.getActionCommand().equals(miNew.getActionCommand())) doNew();
            else if (newEvent.getActionCommand().equals(miOpen.getActionCommand())) doOpen();
            else if (newEvent.getActionCommand().equals(miClose.getActionCommand())) doClose();
            else if (newEvent.getActionCommand().equals(miSave.getActionCommand())) doSave();
            else if (newEvent.getActionCommand().equals(miSaveAs.getActionCommand())) doSaveAs();
            else if (newEvent.getActionCommand().equals(miUndo.getActionCommand())) doUndo();
            else if (newEvent.getActionCommand().equals(miCut.getActionCommand())) doCut();
            else if (newEvent.getActionCommand().equals(miCopy.getActionCommand())) doCopy();
            else if (newEvent.getActionCommand().equals(miPaste.getActionCommand())) doPaste();
            else if (newEvent.getActionCommand().equals(miClear.getActionCommand())) doClear();
            else if (newEvent.getActionCommand().equals(miSelectAll.getActionCommand())) doSelectAll();
        public void doNew() {}
        public void doOpen() {}
        public void doClose() {}
        public void doSave() {}
        public void doSaveAs() {}
        public void doUndo() {}
        public void doCut() {}
        public void doCopy() {}
        public void doPaste() {}
        public void doClear() {}
        public void doSelectAll() {}
        public static void main(String args[]) {
            new test();
    }������������������������������������������
    and the coe of the aboutbox
    ������������������������������������������
    //     File:     AboutBox.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class AboutBox extends JFrame
                          implements ActionListener
        protected JButton okButton;
        protected JLabel aboutText;
        public AboutBox() {
         super();
            this.getContentPane().setLayout(new BorderLayout(15, 15));
            this.setFont(new Font ("SansSerif", Font.BOLD, 14));
            aboutText = new JLabel ("About test");
            JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            textPanel.add(aboutText);
            this.getContentPane().add (textPanel, BorderLayout.NORTH);
            okButton = new JButton("OK");
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
            buttonPanel.add (okButton);
            okButton.addActionListener(this);
            this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            this.pack();
        public void actionPerformed(ActionEvent newEvent) {
            setVisible(false);
    }thanks

    please anyone wanna help me?

  • Urgent Help on a Java Menus

    I am doing this simple program. where i have created three menu's and i want it to pop-up a seperate window when i press any of the menu items. till now i have written this code and created the menus, can you please tell me how to attach actionlisteners to each of the menu items, so that when one menuitem is selected it leads to a another frame performing a different function. It is also not showing the color i am using in the code.
    public class GUIform extends JMenuBar {
    String[] fileList = new String[] {"Connect", "Exit"};
    String[] dataList = new String[] {"Query", "Add Student", "Delete Student", "Update Student"};
    String[] OptionsList = new String[] {"Color", "Font"};
    char[] fileShortcuts = {'L', 'E'};
    char[] dataShortcuts = {'Q', 'A', 'T', 'U'};
    char[] OptionsShortcuts = {'C', 'N'};
    /** Creates a new instance of GUIform */
    public GUIform() {
    JMenu fileMenu = new JMenu("File");
    JMenu dataMenu = new JMenu("Data");
    JMenu OptionsMenu = new JMenu("Options");
    ActionListener printListener = new ActionListener(){
    public void actionPerformed(ActionEvent event) {
    System.out.println("Menu item [" + event.getActionCommand( ) +
    "] was pressed.");
    for(int i = 0; i < fileList.length; i++)
    JMenuItem link = new JMenuItem(fileList, fileShortcuts);
    link.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts,
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
    link.addActionListener(printListener);
    fileMenu.add(link);
    for(int i = 0; i < dataList.length; i++)
    JMenuItem link = new JMenuItem(dataList, dataShortcuts);
    link.setAccelerator(KeyStroke.getKeyStroke(dataShortcuts,
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
    link.addActionListener(printListener);
    dataMenu.add(link);
    for(int i = 0; i < OptionsList.length; i++)
    JMenuItem link = new JMenuItem(OptionsList, OptionsShortcuts);
    link.setAccelerator(KeyStroke.getKeyStroke(OptionsShortcuts,
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
    link.addActionListener(printListener);
    OptionsMenu.add(link);
    add(fileMenu);
    add(dataMenu);
    add(OptionsMenu);
    public static void main(String s[ ]) {
    JFrame frame = new JFrame("Student Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(new GUIform( ));
    JPanel panel = new JPanel();
    panel.setBackground(Color.ORANGE);
    frame.pack( );
    frame.setSize(300, 200);
    frame.setLocation(200, 200);
    frame.setVisible(true);

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MenuTest
        public static void main(String s[ ])
            JFrame frame = new JFrame("Student Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setJMenuBar(getMenuBar( ));
            JPanel panel = new JPanel();
            panel.setBackground(Color.ORANGE);
            frame.getContentPane().add(panel);
            frame.pack( );
            frame.setSize(300, 200);
            frame.setLocation(200, 200);
            frame.setVisible(true);
        private static JMenuBar getMenuBar()
            String[] fileList = { "Connect", "Exit" };
            String[] dataList = {
                "Query", "Add Student", "Delete Student", "Update Student"
            String[] OptionsList = { "Color", "Font" };
            char[] fileShortcuts = {'L', 'E'};
            char[] dataShortcuts = {'Q', 'A', 'T', 'U'};
            char[] OptionsShortcuts = {'C', 'N'};
            JMenu fileMenu = new JMenu("File");
            JMenu dataMenu = new JMenu("Data");
            JMenu OptionsMenu = new JMenu("Options");
            ActionListener printListener = new ActionListener()
                public void actionPerformed(ActionEvent event)
                    JMenuItem item = (JMenuItem)event.getSource();
                    String ac = item.getActionCommand();
                    System.out.println("ac = " + ac);
            for(int i = 0; i < fileList.length; i++)
                JMenuItem link = new JMenuItem(fileList, fileShortcuts[i]);
    link.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts[i],
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask( ), false));
    link.setActionCommand(fileList[i]);
    link.addActionListener(printListener);
    fileMenu.add(link);
    for(int i = 0; i < dataList.length; i++)
    JMenuItem link = new JMenuItem(dataList[i], dataShortcuts[i]);
    link.setAccelerator(KeyStroke.getKeyStroke(dataShortcuts[i],
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
    link.setActionCommand(dataList[i]);
    link.addActionListener(printListener);
    dataMenu.add(link);
    for(int i = 0; i < OptionsList.length; i++)
    JMenuItem link = new JMenuItem(OptionsList[i], OptionsShortcuts[i]);
    link.setAccelerator(KeyStroke.getKeyStroke(OptionsShortcuts[i],
    Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
    link.setActionCommand(OptionsList[i]);
    link.addActionListener(printListener);
    OptionsMenu.add(link);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(dataMenu);
    menuBar.add(OptionsMenu);
    return menuBar;

  • How do you add a menus to a menu bar???

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the FormEditor.
    I can't edit the code
    (eg. private void initComponents() {
    menuBar = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    saveMenuItem = new javax.swing.JMenuItem();
    following the above comments...
    can anyone tell me how to add menus to a menu bar???
    I am very new to Forte ...

    In the form editor, select the JMenuBar in your form, right click on it and you will see a menu item call Add which will display a sub-menu for things like JMenuItem and so on.
    The reason why Forte prevents you from editing those portions of the source code is because Forte completely rewrites those portions automatically. Any changes you may have made outside of Forte will then be lost.

  • Menus appearing underneath other components

    I am having a problem making my menus appear on top when they 'pop up'. I know this is a problem with mixing heavyweight and lightweight components, and I have been trying to force the menus to be heavyweight but to no avail.
    I have been trying to set the menus to be heavyweight by calling this method after the menu has been instantiated:
    [menuname].getPopupComponent().lightWeightPopupEnabled(false);
    Here is an image showing my problem:
    http://www.inf.brad.ac.uk/~jpcatter/menus.jpg
    Edited by: JonCat on Jul 26, 2008 8:47 AM

    It is not a mix of swing and awt. The browser component is the mozswing MozillaPanel component, and it is this that is being drawn on top of my menus, as can be seen from the screenshot that I posted. Another thought I had was that the browser component is the last to be drawn, as it is added to the tab control once all other components are drawn. Could it be the Z ordering of components causing the overlap, or is it strictly a heavy / light weight issue?
    Here is a link to the mozswing homepage for anyone who is interested:
    http://confluence.concord.org/display/MZSW/Home

  • Create a popup window in Swing

    Hi, I want to create a popup window that contains some buttons and text fields when the user selects a menuItem from the menuBar using Swing. What would be the best way to do this?

    How to Use Menus:
    http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
    In your actionPerformed code you would create and display a JDialog.

  • Swing and Skelmir VM

    Hello Everyone,
    I'm using a proprietary VM (from Skelmir) on a Linux/XScale board, and I'm having problems setting up the proper Swing classes my app needs to use.
    Has anyone dealt with this issue yet ?
    Any direction is appreciated.
    Regards,
    Andre

    Not a reply, abit more info:-
    After some more testing,
    On my sytstem is gets to the point where all the menus/menuitems are displayable...
    the menuitems will drop down........
    Now the fun part......,
    sometimes if I click an item enough times (or sometimes even the first time) they fire off to the listeners.
    Other times they just don't respond.
    So it is probably a simple internal JAVA problem but most likely an environmental one.
    Anyone know a forum where this might get answered (meaining it is probably an external to JAVA thing and not that this forum isn't useful).
    Thanks.

Maybe you are looking for

  • Can I print a document comparison in Acrobat 9?

    Our customer requires a pdf comparison report for any files we revise, in Acrobat 8 we can print a side-by-side report, in Acrobat 9 it appears that you can only view the comparison on screen. I'm hoping I'm missing something really obvious - can any

  • Never made web site,Not subscribed to MobileMe, where can  publish for free

    I want to start off making a website, a basic one, as its my 1st about a hobby. I was going to use a free online web based one, but thought I have iMac and iLife 08, so why not try iWeb I know its tied into MobileMe, but is there a way to post the si

  • HELP ! Ipod only plays out of left headphone!

    i have tried everything. i have aquired new headphones. and the problem still occurs. i have even tried hooking it up to my car radio via Belkin tape adapter and it only play out of the left side speakers in my car. what is the problem !? PLEASE I NE

  • A script makes Adobe Flash 10 slow and must be aborted--Why?

    The messages says: A script in this movie is causing Adobe Flash Player 10 to run slowly. If it continues to run your computer may become unresponsive. Do you want to abort the script? I am using iWeb '08 and trying to embed a photo and viewer from a

  • Error code 4?

    I want to update my phone but ovi keeps saying error code 4 and wont work!??!! whats wrong?