Using JMenu and JMenuItem

I have a JMenu attached to a JFrame, along with a panel and a graphic object which extends Canvas. When I click the Menu bar, the menu does not show, it actually falls behind the Canvas. I can see it if I remove the Canvas. How do you make it appear over the Canvas or the items drawn over the Canvas?
Any help will be greatly appreciated.
Pradeep Gupta
[email protected]
Mon 03Nov03 10:31

You are mixing heavyweight and lightweight components. Can you subclass JComponent or JPanel instead of Canvas? That would solve your problem.

Similar Messages

  • JMenu and JMenuItem - Images

    How to put an image in front of the JMenu text?
    JMenuItem has a constructor to insert the icon.
    Is their a reason why it not in the JMenu constructors?
    Sometimes I just want to put a 16 pixel gap in front of the JMenuItem or JMenu if it has no Image. Can one do that without inserting a blank
    Image?
    Thanks

    Have you tried just setting it via setIcon()? Since both JMenu and
    JMenuItem inherit from AbstractButton, this may work; I say may
    since more than likely it is up to the L&F UI to do the right thing.
    A quick browse shows that it ought to work for at least the basic
    L&Fs though.
    As far as the empty gap, looks like you might be out of luck by
    default, but this too is up to the L&F. For instance, some look and
    feels line up the text properly if an item doesn't have an icon.
    As a quick fix, though, you could probably just set the icon to
    something like
    menuItem.setIcon( new Icon() {
        public int getIconWidth() { return 16; }
        public int getIconHeight() { return 0; }
        public void paintIcon(Component c, Graphics g, int x, int y) {}
    });: jay

  • JMenu and JMenuItem

    Is it possible to have a JMenuItem within a JMenuItem? For example: Menu would drop down to a JMenuItem with two choices and when you click on one of the two choices another JMenuItem would have more choices available?

    Read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html]How to Use Menus for an example.

  • JMenu and JMenuItems

    hi
    does anybody know how i can disable menuitems
    and enable them on some given events?
    would help me very much!
    thanx

    A good way to do this is to use Actions - define an Action object for each menu item (including text and an icon if you want), then just add the actions to the menu, and it will show them with icons automatically.
    Then if you do setEnabled(false) on the action, the menu item is greyed out. Also, you can add these Actions to other components such as a JToolBar, which would automatically render them as buttons (these also get greyed out when disabed).
    The big benefit is that all your "action" code is in one place, so the menu and the toolbar both call the same code.
    For a tutorial, see
    http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

  • Custom text for JMenu and JMenuItem

    hey guys
    right now i can do a :
    JMenu menu = new JMenu("File");but how can i customize the text on the Menu a.k.a "file" so may be it looks bigger or a different color ?
    ive looked at the API i can see that JMenu takes "Action" object too which describes the look. I know Action is an interface and i havent been able to find a suitable class that implements this interface
    so how should i go about changing the text look/feel ?
    thank in advance

    so may be it looks bigger or a different color ?Read the API. There are many different set??? methods which allow you to change the properties of the component including the font size and color of the text.
    i can see that JMenu takes "Action" object too which describes the look.The Action has nothing to do with the look (other then providing the text to display). It has to do with the Action that happens when you select the menu item.

  • JMenu and MouseListener problems

    I have a JMenu that is used as a JMenuItem in another JMenu in the menu bar. The same JMenu is added as a popup menu to a button.
    I added a MouseListener to the JMenu because I want to execute some code upon mouseEntered() and mouseExited().
    Problem: the listener is only fired when I enter or exit the menu from the menu bar, never when the popup menu of the button is entered or exited.
    What can I do to trigger the mouse listener?

    I have a JMenu that is used as a JMenuItem in another JMenu in the menu bar.Components can only have a single parent. I don't know how what you are describing is possible.
    If you want to share the ActionEvent then you should be using an Action when creating the menu. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html]How to use Actions.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • Using KeyEvents and Listener

    I am using JTextFields and i want when enter is pushed to check to see if the username and password is right. How do i do this i have read the api but i still don't understand.
    Here is my code and thanx for any help (note i have no features yet it is just a interface):
    *Diary
    *@Collin Doering
    *Version 1.00
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class diary extends JFrame implements KeyListener, ActionListener {
         private JLabel user, pass;
         private JTextField username;
         private JPasswordField password;
         private JMenuBar menu = new JMenuBar();
         private JMenu file, edit, font, background, tcolor, help;
         private JMenuItem open, save, saveas, exit, b8, b10, b12, b14, b16, b18, b20, b24, b30, bblue, bred, bgreen, cblue, cred, cgreen, using, about;
         public diary() {
              setSize(new Dimension(500, 500));
              setTitle("Diary 1.00");
              file = new JMenu("File");
              menu.add(file);
              open = new JMenuItem("Open");
              open.addActionListener(this);
              file.add(open);
              save = new JMenuItem("Save");
              save.addActionListener(this);
              file.add(save);
              saveas = new JMenuItem("Save as");
              saveas.addActionListener(this);
              file.add(saveas);
              exit = new JMenuItem("exit");
              exit.addActionListener(this);
              file.add(exit);
              edit = new JMenu("Edit");
              menu.add(edit);
              font = new JMenu("font");
              edit.add(font);
              b8 = new JMenuItem("8");
              b8.addActionListener(this);
              font.add(b8);
              b10 = new JMenuItem("10");
              b10.addActionListener(this);
              font.add(b10);
              b12 = new JMenuItem("12");
              b12.addActionListener(this);
              font.add(b12);
              b14 = new JMenuItem("14");
              b14.addActionListener(this);
              font.add(b14);
              b16 = new JMenuItem("16");
              b16.addActionListener(this);
              font.add(b16);
              b18 = new JMenuItem("18");
              b18.addActionListener(this);
              font.add(b18);
              b20 = new JMenuItem("20");
              b20.addActionListener(this);
              font.add(b20);
              b24 = new JMenuItem("24");
              b24.addActionListener(this);
              font.add(b24);
              b30 = new JMenuItem("24");
              b30.addActionListener(this);
              font.add(b30);
              background = new JMenu("background");
              edit.add(background);
              bblue = new JMenuItem("blue");
              bblue.addActionListener(this);
              background.add(bblue);
              bred = new JMenuItem("red");
              bred.addActionListener(this);
              background.add(bred);
              bgreen = new JMenuItem("green");
              bgreen.addActionListener(this);
              background.add(bgreen);
              tcolor = new JMenu("text color");
              edit.add(tcolor);
              cblue = new JMenuItem("blue");
              cblue.addActionListener(this);
              tcolor.add(cblue);
              cred = new JMenuItem("red");
              cred.addActionListener(this);
              tcolor.add(cred);
              cgreen = new JMenuItem("green");
              cgreen.addActionListener(this);
              tcolor.add(cgreen);
              help = new JMenu("Help");
              menu.add(help);
              using = new JMenuItem("Using");
              using.addActionListener(this);
              help.add(using);
              about = new JMenuItem("About");
              about.addActionListener(this);
              help.add(about);
              user = new JLabel("username:");
              pass = new JLabel("password:");
              username = new JTextField("");
              password = new JPasswordField("");
              password.addKeyListener(this);
              JPanel southpane = new JPanel(new GridLayout(2, 2));
              southpane.add(user);
              southpane.add(pass);
              southpane.add(username);
              southpane.add(password);
              JPanel main = new JPanel(new GridLayout(0, 1));
              getContentPane().add(menu, BorderLayout.NORTH);
              getContentPane().add(southpane, BorderLayout.SOUTH);
        public void keyTyped(KeyEvent e) {
             //coming
        public void keyPressed(KeyEvent e) {
             //coming
        public void keyReleased(KeyEvent e) {
             //coming
         public void actionPerformed(ActionEvent e) {
              //coming
         public static void main(String args[]) {
              new diary().setVisible(true);
    }

    here's a rough login
    the username/password is hard-coded, modify this to whatever you intend having
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Login extends JDialog
      String validUsername = "collin";
      String validPassword = "java";
      public Login()
        setTitle("Log in");
        setSize(250,125);
        setLocation(400,300);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(3,2));
        getContentPane().add(new JLabel("User name: "));
        final JTextField username = new JTextField();
        getContentPane().add(username);
        getContentPane().add(new JLabel("Password: "));
        final JPasswordField password = new JPasswordField(10);
        getContentPane().add(password);
        final JButton okBtn = new JButton("OK");
        getContentPane().add(okBtn);
        final JButton cancelBtn = new JButton("Cancel");
        getContentPane().add(cancelBtn);
        okBtn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            if(username.getText().trim().toLowerCase().equals(validUsername) &&
               (new String(password.getPassword())).trim().toLowerCase().equals(validPassword))
              new Diary().setVisible(true);
              dispose();
            else JOptionPane.showMessageDialog(getContentPane(),"invalid username/password");}});
        cancelBtn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            System.exit(0);}});
      public static void main(String[] args){new Login().setVisible(true);}
    class Diary extends JFrame implements ActionListener
      private JLabel user, pass;
      private JMenuBar menu = new JMenuBar();
      private JMenu file, edit, font, background, tcolor, help;
      private JMenuItem open, save, saveas, exit, b8, b10, b12, b14, b16, b18, b20,
                  b24, b30, bblue, bred, bgreen, cblue, cred, cgreen, using, about;
      public Diary()
        setSize(new Dimension(500, 500));
        setTitle("Diary 1.00");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(300,100);
        file = new JMenu("File");
        menu.add(file);
        open = new JMenuItem("Open");
        open.addActionListener(this);
        file.add(open);
        save = new JMenuItem("Save");
        save.addActionListener(this);
        file.add(save);
        saveas = new JMenuItem("Save as");
        saveas.addActionListener(this);
        file.add(saveas);
        exit = new JMenuItem("exit");
        exit.addActionListener(this);
        file.add(exit);
        edit = new JMenu("Edit");
        menu.add(edit);
        font = new JMenu("font");
        edit.add(font);
        b8 = new JMenuItem("8");
        b8.addActionListener(this);
        font.add(b8);
        b10 = new JMenuItem("10");
        b10.addActionListener(this);
        font.add(b10);
        b12 = new JMenuItem("12");
        b12.addActionListener(this);
        font.add(b12);
        b14 = new JMenuItem("14");
        b14.addActionListener(this);
        font.add(b14);
        b16 = new JMenuItem("16");
        b16.addActionListener(this);
        font.add(b16);
        b18 = new JMenuItem("18");
        b18.addActionListener(this);
        font.add(b18);
        b20 = new JMenuItem("20");
        b20.addActionListener(this);
        font.add(b20);
        b24 = new JMenuItem("24");
        b24.addActionListener(this);
        font.add(b24);
        b30 = new JMenuItem("24");
        b30.addActionListener(this);
        font.add(b30);
        background = new JMenu("background");
        edit.add(background);
        bblue = new JMenuItem("blue");
        bblue.addActionListener(this);
        background.add(bblue);
        bred = new JMenuItem("red");
        bred.addActionListener(this);
        background.add(bred);
        bgreen = new JMenuItem("green");
        bgreen.addActionListener(this);
        background.add(bgreen);
        tcolor = new JMenu("text color");
        edit.add(tcolor);
        cblue = new JMenuItem("blue");
        cblue.addActionListener(this);
        tcolor.add(cblue);
        cred = new JMenuItem("red");
        cred.addActionListener(this);
        tcolor.add(cred);
        cgreen = new JMenuItem("green");
        cgreen.addActionListener(this);
        tcolor.add(cgreen);
        help = new JMenu("Help");
        menu.add(help);
        using = new JMenuItem("Using");
        using.addActionListener(this);
        help.add(using);
        about = new JMenuItem("About");
        about.addActionListener(this);
        help.add(about);
        //getContentPane().add(menu, BorderLayout.NORTH);//this will cause you problems later on
        setJMenuBar(menu);
      public void actionPerformed(ActionEvent ae){}
    }

  • Accessing JMenu through JMenuItem

    How can I get access to the JMenu object having only JMenuItem contained in it?
    JMenu menu = new JMenu("Menu");
    JMenuItem menuItem = new JMenuItem("Menu Item");
    menu.add(menuItem);
    // Here I have only reference to 'menuItem'
    Thanks,
    TTauruSS

    looks like you can go down but not up.
    If you have a group of JMenu's then just go through each one, and use
        MenuElement []comps;
        if(menu.isMenuComponent(myMenuItem)) {
    // its in here...
    comps = menu[i].getSubElements();
    combination of the above should let you find what your looking for. Note that searching through a menu lends itself to some recursive coding.
    James.

  • Problem with Alt , JMenu and Mnemonics

    I have built an application with a JMenuBar containing several JMenu's, the first of which is a File menu. All JMenus and JMenuItems have mnemonics associated with them. When I press the alt key the underlines show and focus appears to be given to the File menu (though it has not dropped down). If I then press a key, for example 'S', that is a mnemonic of the 'Save' jmenuitem in the File menu, the Save action is invoked. This should not occur because the menu has not opened yet.
    The behavior is what I'd expect had an accelerator (Alt+S) been defined for the Save menu item. But I have not defined any accelerators.
    Why does this happen and more importantly, is there a work around?

    Except for the 1st line in jbinit() it's all pretty standard stuff. Here's a snippet of the code:
    public class MainFrame extends JFrame implements ChangeListener {
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileNew = new JMenuItem();
    JMenuItem jMenuFileSave = new JMenuItem();
    JMenu m_editMenu = new JMenu();
    JMenuItem m_editCutMenuItem = new JMenuItem();
    JMenuItem m_editCopyMenuItem = new JMenuItem();
    JMenuItem m_editPasteMenuItem = new JMenuItem();
    public MainFrame() {
    jbInit();
    //Component initialization
    private void jbInit() {
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke(KeyEvent.VK_ALT, Event.ALT_MASK, false), "repaint");
    // file menu
    jMenuFile.setText("File");
    jMenuFile.setMnemonic(KeyEvent.VK_F);
    jMenuFileNew.setText("New...");
    jMenuFileNew.setMnemonic(KeyEvent.VK_N);
    jMenuFileNew.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileNewSpecial_actionPerformed(e);
    jMenuFileSave.setText("Save");
    jMenuFileSave.setMnemonic(KeyEvent.VK_S);
    jMenuFileSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileSaveSpecial_actionPerformed(e);
    jMenuFile.add(jMenuFileNew);
    jMenuFile.add(jMenuFileSave);
    // edit menu
    m_editMenu.setText("Edit");
    m_editMenu.setMnemonic(KeyEvent.VK_E);
    m_editCutMenuItem.setText("Cut");
    m_editCutMenuItem.setMnemonic(KeyEvent.VK_T);
    m_editCutMenuItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    editCutMenuItem_actionPerformed(e);
    m_editCopyMenuItem.setText("Copy");
    m_editCopyMenuItem.setMnemonic(KeyEvent.VK_C);
    m_editPasteMenuItem.setText("Paste");
    m_editPasteMenuItem.setMnemonic(KeyEvent.VK_P);
    m_editMenu.add(m_editCutMenuItem);
    m_editMenu.add(m_editCopyMenuItem);
    m_editMenu.add(m_editPasteMenuItem);
    jMenuBar1.add(jMenuFile);
    jMenuBar1.add(m_editMenu);
    this.setJMenuBar(jMenuBar1);
    etc...
    Pressing Alt+S invokes the action listener for the jMenuFileSave menu item. It should do nothing since there is no top level menu with a mnemonic of 'S'.

  • MouseEvents on JPopupMenu and JMenuItem

    Hi
    I am trying to get the mouse events from JPopupMenu
    and JMenuItem , but nothing happens
    I display the popup once a button is pressed , the popup is displayed but once I click it there is no response
    can some one help
    I am attaching the code
    import javax.swing.*;
    import java.awt.event.*;
    public class Pop extends JFrame implements ActionListener{
         JPopupMenu popup = new JPopupMenu();
         JMenuItem item = new JMenuItem("one");
         JMenuItem item2 = new JMenuItem("two");
         JButton ok = new JButton("ok");
         Pop(){
              H hand = new H();
              popup.add(item);
              popup.add(item2);
              popup.addMouseListener(hand);
              item.addMouseListener(hand);
              item2.addMouseListener(hand);
              ok.addActionListener(this);
              getContentPane().add(ok);
              setSize(300,300);
              show();
         public void actionPerformed(ActionEvent ee){
              popup.show(ok , 5,5);
         public static void main(String[] args){
              new Pop();
         class H extends MouseAdapter{
              public void mouseClicked (MouseEvent e){
                   JOptionPane.showMessageDialog(null,e.toString());
    }

    If you are just trying to get something done when a menuitem is clicked, then you can try to use actionListener instead of the mouselistener. Try the following:
    ActionListener al = new ActionListener()
    public void actionPerformed()
    OptionPane.showMessageDialog(null,e.toString());
    item.addActionListener(al);
    item2.addActionListener(al2);
    Now when you click an item, the dialog box would be shown.

  • JMenu and making the popup always visible

    hi,
    i need to keep the menu's popup visible always and eventually the selection of an item even when the focus moves to another component-a text field nearby for example.can this (and how if it is) be achieved or it is totally up to the look and feel and i can't do much about it?
    thank you.

    just for the record,it seems it actually is doable.at least i have a prototype working.it involves removing default mouse listeners from jmenu i jmenuitem and installing "custom" ones that manage the transition of the menu from one state to another.

  • JMenu and Documentation

    Hello friends, couple questions here. Main one i am confused with is the Documentations. I cant quite seem to grasp how to use what they provide. If anyone would be so kind as to take the time and explain to me how to use what the documentation offers i would be grateful.
    http://wwwswt.fzi.de/~jjh/tools/swing/products/jfc/swingdoc-current/api/com.sun.java.swing.JMenuBar.html
    here is a link to a documentation of JMenu and how to make one but i do not get how to apply this into a program. Thank you very much - Jeff

    Specificially, you want this page
    http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
    This is part of a much bigger tutorial, here
    http://java.sun.com/docs/books/tutorial/
    There are also more tutorials if you click on Tutorials in the upper left of this page.

  • JMenu.add(JMenuItem)

    Can I just say to start with. I pressed the browser back button from the preview page and it lost my whole post. How annoying is that?
    Anyway.
    Does anyone have any idea why the JMenu.add(JmenuItem) method might not work? It does not throw any exceptions, the menu item is simply not added. I basically have 5 lines of very similar code, the first 2 items are added fine, but the last 3 are not. I have stepped through the code and the last 3 menu itmes look fine, they just dont appear in the menu. I am totally stuck. Please help

    Hello,
    if you have code like this.JMenu menu=new JMenu("Menu");
    JMenuItem item=new JMenuItem("item");
    menu.add(item);
    menu.add(item);only the last item will be visible in the menu and the first will be removed.
    Swing is looking if the component you want to add to a container equals any item in the component hyrarchy .
    Regards,
    Tim

  • Using NULL and NOT NULL in prompted filters

    Dear all,
    While trying to grap the concept of prompted filters in sap bo web intelligence, I had a question whether why we cannot use NULL and NOT NULL while creating a prompted filters in our report.

    HI,
    'Is Null' and 'Not Null' are the predefined functions in webi which only eliminate the null values or considering only null values.
    'Is Null' and 'Not Null' are itself predefined functions that why you are not getting  prompts.
    Null values are standard across the databases so this is defined  as a function in webi to specific eliminate the null values.
    If something is not standard then there is option in the webi to use different operator with static values or with prompts.
    More more information on Null see the Null wiki page.
    Null (SQL) - Wikipedia, the free encyclopedia
    Amit

  • How do I use Qt and OpenGL with Visual Studio

    Hi! I mainly want to program in C++ and I want to use Qt and OpenGL with Visual Studio.
    I am currently revising C++ and later on i am going to start reading Qt and OpenGL. I have a background of
    Embedded firmware design(C and Assembly).
    The Visual Studio Version I have is 2013 ultimate. How do I use Qt and OpenGL with Visual Studio?
    Thanks
    Alexandros

    Hi ClassicalGuitar,
    The forum supports VS setup and installation. And your issue is not about the forum. I will move the thread to off-topic forum. Thanks for your understanding.
    Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

Maybe you are looking for

  • User Error Needs Help Correcting - Repeatable Tag on Custom Section Attribute

    OK, I have v.6111.  Today I am manually putting the supplier_eq_create tag on all the the existing attributes in our custom sections so suppliers can add them when we send eQs. Unfortuately, I accidently clicked on the Repeatable tag instead of Suppl

  • Sun Java Communications Suite questions

    Hi all! This is my first post to these boards so please excuse any board fobars. Anyway, I have been charge with replacing our existing mail system based on POSTFIX, IMAP and SMTP with a better solution and have come to Sun Java Communications Suite

  • Impossible to detect an AUTO_INCREMENT column in a table of PostgreSQL DB.

    Hello, I need to detect an AUTO_INCREMENT column in a table of PostgreSQL DB. Note : PostgreSQL uses sequence number to create an AUTO_INCREMENT column. From a ResultSetMetaData, I call the methode isAutoIncrement(int index) which returns true if the

  • Mavericks Says "Downloaded" But Won't Install

    As noted in the screenshot below, OS X Mavericks is listed as downloaded (which can't be possible -- it said 3 more hours to download then jumped directly to "downloaded" status).  Is there a way to restart the download from scratch?  At this point,

  • I'm having terrible problems loading websites.......

    .this just started (again) this afternoon.......switched web browsers and everything is fine.......no software updates......what is going on