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

Similar Messages

  • 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.

  • 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

  • I want to set an Action on a JMenu (not JMenuItem) but...

    ...it doesn't seem to work. I can set actions on JMenuItems no problem. But actions and actionlistening doesn't seem to work on JMenus. I can add MouseListener to a JMenu, but I REALLY want an action.
    can anyone help?
    Olly

    JMenu.addActionListener(yourListener) ?

  • 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.

  • 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.

  • JDK1.3 doesn't support JInternalFrame?

    Hi,
    I got one source code file for testing JInternalFrame:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class DesktopTest extends JFrame {
    public DesktopTest()
    super( "Using a JDesktopPane" );
    JMenuBar bar = new JMenuBar();
    JMenu addMenu = new JMenu( "Add" );
    JMenuItem newFrame = new JMenuItem( "Internal Frame" );
    addMenu.add( newFrame );
    bar.add( addMenu );
    setJMenuBar( bar );
    final JDesktopPane theDesktop = new JDesktopPane();
    getContentPane().add( theDesktop );
    newFrame.addActionListener(
    new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    JInternalFrame frame =
    new JInternalFrame(
    "Internal Frame",
    true, true, true, true );
    Container c = frame.getContentPane();
    MyJPanel panel = new MyJPanel();
    c.add( panel, BorderLayout.CENTER );
    frame.setSize(
    panel.getImageWidthHeight().width,
    panel.getImageWidthHeight().height );
    frame.setOpaque( true );
    theDesktop.add( frame );
    setSize( 500, 400 );
    show();
    public static void main( String args[] )
    DesktopTest app = new DesktopTest();
    app.addWindowListener(
    new WindowAdapter() {
    public void windowClosing( WindowEvent e )
    System.exit( 0 );
    class MyJPanel extends JPanel {
    private ImageIcon imgIcon;
    public MyJPanel()
    imgIcon = new ImageIcon( "jhtp3.gif" );
    public void paintComponent( Graphics g )
    imgIcon.paintIcon( this, g, 0, 0 );
    public Dimension getImageWidthHeight()
    return new Dimension( imgIcon.getIconWidth(),
    imgIcon.getIconHeight() );
    The internal frame can show up when I use JBuilder3.0. But if I use JDK1.3 to run
    it, it can't show up. Who can tell me what's wrong with it and what I should do
    to let it work?
    Thank you in advance.

    You'll need frame.setVisible(true).
    I think this was new around 1.3 (previously JInternalFrame defaulted to visible).
    Tony.

  • How do I only allow 1 internal frame to be added?

    Could you pls show me how can I do this, I need some code to learn how this code work.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Test1a extends JFrame
         private JLabel label1;
    //     private JLabel label2;
         private JLabel label3;
         private JLabel label4;
         private JLabel label5;
         private JDesktopPane theDesktop;
         public Test1a()
              super("TESTING JLABEL");
              setLayout(new BorderLayout(5, 5));
            JMenuBar bar = new JMenuBar();
          JMenu addMenu = new JMenu( "Add" );
          JMenuItem newFrame = new JMenuItem( "Internal Frame" );
          addMenu.add( newFrame );
          bar.add( addMenu );
          setJMenuBar( bar );
           theDesktop = new JDesktopPane();
          add( theDesktop, BorderLayout.CENTER );
          newFrame.addActionListener(
             new ActionListener()
                // display new internal window
                public void actionPerformed( ActionEvent event )
                   // create internal frame
                   JInternalFrame frame = new JInternalFrame("Internal Frame", true, true, true, true);
                   MyJPanel panel = new MyJPanel();
                   frame.add( panel, BorderLayout.CENTER );
                   frame.pack(); // set internal frame to size of contents
                   theDesktop.add( frame );
                   frame.setVisible( true );
              label1= new JLabel ();
              label1.setText("Label 1");
              add(label1, BorderLayout.WEST);
         //     label2= new JLabel ("Label 2", SwingConstants.CENTER);
         //   add(label2, BorderLayout.CENTER);
              label3= new JLabel ();
              label3.setText("Label 3");
              add(label3, BorderLayout.EAST);
              label4= new JLabel ("Label 4", SwingConstants.CENTER);
              add(label4, BorderLayout.NORTH);
              label5= new JLabel ("Label 5", SwingConstants.CENTER);
              add(label5, BorderLayout.SOUTH);
    =============================================
    import java.awt.*;
    import javax.swing.*;
    // class to display an ImageIcon on a panel
    class MyJPanel extends JPanel
       private ImageIcon imageIcon;
       // load image
       public MyJPanel()
          imageIcon = new ImageIcon("yellowflowers.png");
       // display imageIcon on panel
       public void paintComponent( Graphics g )
          super.paintComponent( g );
          imageIcon.paintIcon( this, g, 0, 0 );
       public Dimension getPreferredSize()
          return new Dimension( imageIcon.getIconWidth(), imageIcon.getIconHeight() ); 
    ========================================
    import javax.swing.*;
    public class Test1b
         public static void main (String args[])
              Test1a test1a = new Test1a();
              test1a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test1a.setSize(800, 600);
              test1a.setVisible(true);
    }

    You where given an answer in your original posting:
    http://forum.java.sun.com/thread.jspa?threadID=5146301

  • Creating a simple splash screen

    Hi,
    I'm trying to create a simple splash screen with maybe a button. When this is pressed, the next thing shown in the application window should be the cylinder with my picture of Arizona on it.
    Can anyone help?
    Also trying to place a new image onto the cylinder shape when the Open button is pressed in the FileChooser dialog box.
    Could someone help with this problem, I'm not sure what else is required.
    Thanks.
    import com.sun.j3d.utils.geometry.Cylinder;
    import com.sun.j3d.utils.image.*;
    import com.sun.j3d.utils.universe.*;
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import com.sun.j3d.utils.applet.MainFrame;
    import java.io.*;
    import java.util.*;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    import java.awt.Container;
    import javax.media.j3d.Group;
    import com.sun.j3d.utils.image.TextureLoader;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar.*;
    import javax.swing.JMenuItem;
    //import javax.swing.JSlider;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.WindowAdapter;
    public class ProjectCylinder extends JApplet
    // Variable declarations
    JMenuBar menuBar;
    JMenu jMenu, jSubMenu;
    JCheckBoxMenuItem cbMenuItem;
    JRadioButtonMenuItem rbMenuItem;
    Container C=getContentPane();
    //JSlider sliderbar;
    // To setup the menu bar, options etc.
    public void init()
    new ProjectCylinder();
    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    menuBar = new JMenuBar();
    menuBar.setPreferredSize(new Dimension(400, 20));
    setJMenuBar(menuBar);
    JMenuItem jMenuItem;
    // Building the File menu
    jMenu = new JMenu("File");
    jMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(jMenu);
    jMenuItem = new JMenuItem("Open...", new ImageIcon("A:/open.gif"));
    jMenuItem.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    int returnVal = fc.showOpenDialog(ProjectCylinder.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    try
    File file = fc.getSelectedFile();
    // in here is supposed to be the functionality of putting a new image onto the cylinder
    catch(FileNotFoundException f)
    // do something here
    jMenuItem.setMnemonic(KeyEvent.VK_O);
    jMenuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_T, ActionEvent.ALT_MASK));
    jMenu.add(jMenuItem);
    jMenu.addSeparator();
    // File Save
    jMenuItem = new JMenuItem("Save", new ImageIcon("A:/save.gif"));
    jMenuItem.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    int returnVal = fc.showSaveDialog(ProjectCylinder.this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    File file = fc.getSelectedFile();
    //this is where a real application would save the file.
    jMenuItem.setMnemonic(KeyEvent.VK_O);
    jMenuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_T, ActionEvent.ALT_MASK));
    jMenu.add(jMenuItem);
    // File Close
    jMenuItem = new JMenuItem("Close");
    jMenuItem.setMnemonic(KeyEvent.VK_O);
    jMenuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_T, ActionEvent.ALT_MASK));
    jMenu.add(jMenuItem);
    jMenuItem = new JMenuItem("Exit", KeyEvent.VK_T);
    jMenuItem.setMnemonic(KeyEvent.VK_T);
    jMenuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_T, ActionEvent.ALT_MASK));
    jMenu.add(jMenuItem);
    C.add("South", menuBar);
    //sliderbar = new JSlider(JSlider.HORIZONTAL, 0, 30);
    //menuBar.add(sliderbar);
    //C.add("South", menuBar);
    // end init
    public BranchGroup createSceneGraph()
    BranchGroup objRoot = new BranchGroup();
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(1.2); //Size of cylinder
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);
    // Set up the colours
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(0.7f, .15f, .15f);
    // Set up the texture map
    TextureLoader loader = new TextureLoader("A:\\Arizona.jpg","LUMINANCE", new Container());
    Texture texture = loader.getTexture();
    texture.setBoundaryModeS(Texture.WRAP);
    texture.setBoundaryModeT(Texture.WRAP);
    texture.setBoundaryColor( new Color4f( 0.0f, 1.0f, 0.0f, 0.0f ));
    // Set up the texture attributes
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    Appearance ap = new Appearance();
    Material mat = new Material();
    ap.setTexture(texture);
    ap.setTextureAttributes(texAttr);
    //set up the material
    ap.setMaterial(new Material(white, red, white, red, 1.0f));
    // Create a cylinder
    int primflags = Cylinder.GENERATE_NORMALS +Cylinder.GENERATE_TEXTURE_COORDS;
    Cylinder CylinderObj = new Cylinder(0.5f, 0.5f, primflags, ap);
    objTrans.addChild(CylinderObj);
    //Add to scene graph
    BoundingSphere bounds =new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
    //Shine coloured lights
    Color3f lColor1 = new Color3f(0.7f, 0.0f, 0.7f);
    Color3f lColor2 = new Color3f(0.7f, 0.7f, 0.0f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);
    objScale.addChild(lgt1);
    objScale.addChild(lgt2);
    // Let Java 3D perform optimizations on this scene graph.
    objRoot.compile();
    return objRoot;
    } // end BranchGroup createSceneGraph
    public ProjectCylinder()
    C.setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    C.add("Center", c);
    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    SimpleUniverse u = new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);
    } // end ProjectCylinder
    public static void main(String argv[])
    BranchGroup group;
    new MainFrame(new ProjectCylinder(), 300, 300);
    } // end void main
    } // end class ProjectCylinder

    Yeah I thought JWindow should be used, but it's getting it into my project that is the real problem.

  • JInternalFrame help!!! any expert in here?

    How do u put an panel inside the JInternalFrame? And how do you put an panel containing buttons inside that first panel which is inside the JInternalFrame.
    help

    k here is all the full coding of what i am working on.
    // Java core packages
    import java.awt.*;
    import java.awt.event.*;
    // Java extension packages
    import javax.swing.*;
    public class Test extends JFrame {
    private JDesktopPane theDesktop;
    // set up GUI
    public Test()
    super( "test" );
    // create menu bar, menu and menu item
    JMenuBar bar = new JMenuBar();
    JMenu addMenu = new JMenu( "Add" );
    JMenuItem newFrame = new JMenuItem( "Internal Frame" );
    addMenu.add( newFrame );
    bar.add( addMenu );
    setJMenuBar( bar );
    // set up desktop
    theDesktop = new JDesktopPane();
    getContentPane().add( theDesktop );
    // set up listener for newFrame menu item
    newFrame.addActionListener(
    // anonymous inner class to handle menu item event
    new ActionListener() {
    // display new internal window
    public void actionPerformed( ActionEvent event ) {
    // create internal frame
    JInternalFrame frame = new JInternalFrame(
    "Internal Frame", true, true, true, true );
    // attach panel to internal frame content pane
    Container container = frame.getContentPane();
    NewMember panel = new NewMember();
    container.add( panel, BorderLayout.CENTER );
    // set size internal frame to size of its contents
    frame.pack();
    // attach internal frame to desktop and show it
    theDesktop.add( frame );
    frame.setVisible( true );
    } // end anonymous inner class
    ); // end call to addActionListener
    setSize( 600, 440 );
    setVisible( true );
    } // end constructor
    // execute application
    public static void main( String args[] )
    DesktopTest application = new DesktopTest();
    application.setDefaultCloseOperation(
    JFrame.EXIT_ON_CLOSE );
    } // end class DesktopTest
    class NewMember extends JFrame {
         JButton btnCancel = new     JButton("Cancel");
         JButton btnSubmit= new JButton("Submit");
         JLabel lblLastName = new JLabel ("Last Name", JLabel.CENTER);
         JLabel lblFirstName = new JLabel ("First Name", JLabel.CENTER);
         JLabel lblMidle = new JLabel ("Midle Name", JLabel.CENTER);
         JLabel lblAddress = new JLabel ("Address", JLabel.CENTER);
         JLabel lblCity = new JLabel ("City", JLabel.CENTER);
         JLabel lblState = new JLabel ("State", JLabel.CENTER);
         JLabel lblZipCode = new JLabel ("Zip Code", JLabel.CENTER);
         JLabel lblPhone = new JLabel ("Phone Number", JLabel.CENTER);
         JLabel lblCredit = new JLabel ("Credit Card #", JLabel.CENTER);
         JLabel lblFormTile = new JLabel ("VIDEO MEMBERSHIP APPLICATION FORM", JLabel.CENTER);
         JTextField txtLastName = new JTextField();
         JTextField txtFirstName = new JTextField();
         JTextField txtMidle = new JTextField();
         JTextField txtAddress = new JTextField();
         JTextField txtCity = new JTextField();
         JTextField txtState = new JTextField();
         JTextField txtZipCode = new JTextField();
         JTextField txtPhone = new JTextField();
         JTextField txtCredit = new JTextField();
    NewMember() {
         Container c = getContentPane();
         c.setLayout (null);
         c.add(txtLastName);
         c.add(txtFirstName);
         c.add(txtMidle);
         c.add(txtAddress);
         c.add(txtCity);
         c.add(txtState);
         c.add(txtZipCode);
         c.add(txtPhone);
         c.add(txtCredit);
    c.add(lblLastName);
    c.add(lblFirstName);
    c.add(lblMidle);
    c.add(lblAddress);
    c.add(lblCity);
         c.add(lblState);
    c.add(lblZipCode);
    c.add(lblPhone);
    c.add(lblCredit);
    c.add(lblFormTile);
    c.add(btnCancel);
    c.add(btnSubmit);
    lblFormTile.setBounds (35, 0, 400, 40);
    lblLastName.setBounds (30, 50, 150, 30);
    lblFirstName.setBounds (30, 80, 150, 30);
    lblMidle.setBounds (30, 110, 150, 30);
    lblAddress.setBounds (30, 140, 150, 30);
    lblCity.setBounds (30, 170, 150, 30);
    lblState.setBounds (30, 200, 150, 30);
    lblZipCode.setBounds (30, 230, 150, 30);
    lblPhone.setBounds (30, 260, 150, 30);
    lblCredit.setBounds (30, 290, 150, 30);
    txtLastName.setBounds (150, 50, 150, 25);
    txtFirstName.setBounds (150, 80, 150, 25);
    txtMidle.setBounds (150, 110, 150, 25);
    txtAddress.setBounds (150, 140, 150, 25);
    txtCity.setBounds (150, 170, 150, 25);
    txtState.setBounds (150, 200, 150, 25);
    txtZipCode.setBounds (150, 230, 150, 25);
    txtPhone.setBounds (150, 260, 150, 25);
    txtCredit.setBounds (150, 290, 150, 25);
    btnCancel.setBounds (200, 350, 120, 30);
    btnSubmit.setBounds (50, 350, 120, 30);
         setSize(500,450);
         setTitle("VIDEO'S NEW MEMBERSHIP");
         setVisible(true);
         setResizable(false);
    Am I doing it correctily? is this the way one should be doing it in terms of adding a Jpanel inside an internalFrame?

  • Dynamic addition of  JMenuItem in JMenu

    I am trying to create a menu for my project. For that i have added the main menus and for each menu there will be menu item. But the problem is i will get the list of menu items for each menu , only at the runtime of my project. i.e. i wabt to add progrramatically add the menu items. Iam really confused with this. I will appriciate if any one help me on this.
    And also i want to know ..is that possible to get the mouse listener for menu.
    Thank in advance
    Regards,
    SAthish

    You can add and remove menu items from the jmenu dynamically, the only thing you will have to take care of is invoking validate() after each insertion/deletion (it's a bit buggy there). All you have to do is create the items on demand and add (remove) them via the add(JMenuItem) (remove(JMenuItem)) method.
    As for your second question, a JMenu is nothing but a JComponent, so you should be able to add a mouse listener like you would with any other component...

  • Add Drop Shadow to JMenu Problem

    Hi,
    I got this code from "Swing Hacks", for some reason it does not run for me, but throws exceptions. I can not figure out why this is happening. This code straight out of the book, should be runnable. Below is the custom class, then the driver-test class, then the Exception:
    import javax.swing.plaf.basic.*;
    import javax.swing.plaf.*;
    import javax.swing.border.*;
    import javax.swing.*;
    import java.awt.*;
    public class CustomPopupMenuUI extends BasicPopupMenuUI{
         public static ComponentUI createUI(JComponent c){
              return new CustomPopupMenuUI();
         public Popup getPopup(JPopupMenu popup, int x,
                   int y){
              Popup pp = super.getPopup(popup, x,y);
              JPanel panel = (JPanel)popup.getParent();
              panel.setBorder(new ShadowBorder(3,3));
              panel.setOpaque(false);
              return pp;
    class ShadowBorder extends AbstractBorder{
         int xoff,yoff;
         Insets insets;
         public ShadowBorder(int x, int y){
              this.xoff = x;
              this.yoff = y;
              insets = new Insets(0,0,xoff,yoff);
         public Insets getBorderInsets(Component c){
              return insets;
         public void paintBorder(Component comp, Graphics g,
                   int x, int y, int width, int height){
              g.setColor(Color.BLACK);
              g.translate(x,y);
              // draw right side
              g.fillRect(width-xoff,yoff,xoff,height-yoff);
              // draw bottom side
              g.fillRect(xoff,height-yoff,width-xoff,yoff);
              g.translate(-x,-y);
    // Driver Below
    import javax.swing.*;
    import javax.swing.plaf.ComponentUI;
    import java.awt.*;
    public class MenuTest {
         public static void main(String[] args)throws Exception{
              UIManager.put("PopupMenuUI","CustomPopupMenuUI");
              //UIManager.put("MenuItemUI","LucentMenuItemUI");
              JFrame frame = new JFrame();
              JMenuBar mb = new JMenuBar();
              //mb.setUI(new CustomMenuUI());
              frame.setJMenuBar(mb);
              JMenu menu = new JMenu("File");
              mb.add(menu);
              menu.add(new JMenuItem("Open"));
              menu.add(new JMenuItem("Save"));
              menu.add(new JMenuItem("Close"));
              menu.add(new JMenuItem("Exit"));
              menu = new JMenu("Edit");
              mb.add(menu);
              menu.add(new JMenuItem("Cut"));
              menu.add(new JMenuItem("Copy"));
              menu.add(new JMenuItem("Paste"));
              menu.add(new JMenuItem("Paste Special.."));
              frame.getContentPane().setLayout(new BorderLayout());
              frame.getContentPane().add("North",new JButton("Button"));
              frame.getContentPane().add("Center",new JLabel("Label"));
              frame.getContentPane().add("South",new JCheckBox("checkbox"));
              frame.pack();
              frame.setSize(200,150);
              frame.show();
    }Exception:
    UIDefaults.getUI() failed: no ComponentUI class for: javax.swing.JPopupMenu[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,desiredLocationX=0,desiredLocationY=0,label=,lightWeightPopupEnabled=true,margin=,paintBorder=true]
    java.lang.Error
         at javax.swing.UIDefaults.getUIError(Unknown Source)
         at javax.swing.MultiUIDefaults.getUIError(Unknown Source)
         at javax.swing.UIDefaults.getUI(Unknown Source)
         at javax.swing.UIManager.getUI(Unknown Source)
         at javax.swing.JPopupMenu.updateUI(Unknown Source)
         at javax.swing.JPopupMenu.<init>(Unknown Source)
         at javax.swing.JPopupMenu.<init>(Unknown Source)
         at javax.swing.JMenu.ensurePopupMenuCreated(Unknown Source)
         at javax.swing.JMenu.add(Unknown Source)
         at hacks.MenuTest.main(MenuTest.java:23)
    advTHANKSance

    I am also using xp and java version 1.4.2_08, but to no avail.. see below:
    C:\workspace\Swing Hacks\src>java -version
    java version "1.4.2_08"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
    Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
    C:\workspace\Swing Hacks\src>java hacks.MenuTest
    UIDefaults.getUI() failed: no ComponentUI class for: javax.swing.JPopupMenu[,0,0
    ,0x0,invalid,alignmentX=null,alignmentY=null,border=,flags=0,maximumSize=,minimu
    mSize=,preferredSize=,desiredLocationX=0,desiredLocationY=0,label=,lightWeightPo
    pupEnabled=true,margin=,paintBorder=true]
    java.lang.Error
    at javax.swing.UIDefaults.getUIError(UIDefaults.java:689)
    at javax.swing.UIDefaults.getUI(UIDefaults.java:719)
    at javax.swing.UIManager.getUI(UIManager.java:784)
    at javax.swing.JPopupMenu.updateUI(JPopupMenu.java:204)
    at javax.swing.JPopupMenu.<init>(JPopupMenu.java:169)
    at javax.swing.JPopupMenu.<init>(JPopupMenu.java:154)
    at javax.swing.JMenu.ensurePopupMenuCreated(JMenu.java:521)
    at javax.swing.JMenu.add(JMenu.java:556)
    at hacks.MenuTest.main(MenuTest.java:23)
    UIDefaults.getUI() failed: no ComponentUI class for: javax.swing.JPopupMenu[,0,0
    ,0x0,invalid,alignmentX=null,alignmentY=null,border=,flags=0,maximumSize=,minimu
    mSize=,preferredSize=,desiredLocationX=0,desiredLocationY=0,label=,lightWeightPo
    pupEnabled=true,margin=,paintBorder=true]
    java.lang.Error
    at javax.swing.UIDefaults.getUIError(UIDefaults.java:689)
    at javax.swing.UIDefaults.getUI(UIDefaults.java:719)
    at javax.swing.UIManager.getUI(UIManager.java:784)
    at javax.swing.JPopupMenu.updateUI(JPopupMenu.java:204)
    at javax.swing.JPopupMenu.<init>(JPopupMenu.java:169)
    at javax.swing.JPopupMenu.<init>(JPopupMenu.java:154)
    at javax.swing.JMenu.ensurePopupMenuCreated(JMenu.java:521)
    at javax.swing.JMenu.add(JMenu.java:556)
    at hacks.MenuTest.main(MenuTest.java:30)
    I don't understand what the issue could be..
    Can anyone reproduce this error??

  • JMenu with no JMenuItem

    Hi!
    I have a JMenuBar with one JMenu (File) and its JMenuItems.
    I want to add another JMenu (settings) but with no JMenuItems, so when the user clicks on settings a new window is shown.
    Is this possible?

    You need to add a MenuListener to the higher level menu. See attached sample working code.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class MenuTest
      public static void main(String args[])
        new MenuTestFrame();
    class MenuTestFrame extends JFrame implements ActionListener
      JMenuBar mb = new JMenuBar();
      JMenu file = new JMenu("File");
      JMenu edit = new JMenu("Edit");
      JMenu view = new JMenu("View");
      JMenu help = new JMenu("Help");
      JMenuItem fileOpen = new JMenuItem("Open...");
      JSeparator separator = new JSeparator();
      JMenuItem fileSaveAs = new JMenuItem("Save As...");
      JMenuItem editCut = new JMenuItem("Cut");
      JMenuItem editCopy = new JMenuItem("Copy");
      JMenuItem editPaste = new JMenuItem("Paste");
      JMenuItem helpAbout = new JMenuItem("About...");
      MenuTestFrame()
        super();
        /* Components should be added to the container's content pane */
        Container cp = getContentPane();
        /* Add menu items to menus */
        file.add(fileOpen);
        file.add(separator);
        file.add(fileSaveAs);
        edit.add(editCut);
        edit.add(editCopy);
        edit.add(editPaste);
        help.add(helpAbout);
        /* Add menus to menubar */
        mb.add(file);
        mb.add(edit);
        mb.add(view);
        mb.add(help);
        /* Set menubar */
        setJMenuBar(mb);
        /* Add the action listeners */
        fileOpen.addActionListener(this);
        fileSaveAs.addActionListener(this);
        editCut.addActionListener(this);
        editCopy.addActionListener(this);
        editPaste.addActionListener(this);
        helpAbout.addActionListener(this);
        /* Add menu listener */
        view.addMenuListener(new MenuListener() {
          public void menuCanceled(MenuEvent evt) {}
          public void menuDeselected(MenuEvent evt) {}
          public void menuSelected(MenuEvent evt)
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                JOptionPane.showMessageDialog(MenuTestFrame.this,"Menu test dialog!","Message",JOptionPane.INFORMATION_MESSAGE);
        /* Add the window listener */
        addWindowListener(new WindowAdapter()
          public void windowClosing(WindowEvent evt)
            dispose();
            System.exit(0);
        /* Size the frame */
        setSize(200,200);
        /* Center the frame */
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle frameDim = getBounds();
        setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
        /* Show the frame */
        setVisible(true);
      public void actionPerformed(ActionEvent evt)
        Object obj = evt.getSource();
        if (obj == fileOpen);
        else if (obj == fileSaveAs);
        else if (obj == editCut);
        else if (obj == editCopy);
        else if (obj == editPaste);
        else if (obj == helpAbout);
    }

Maybe you are looking for

  • Adobe Air Error message on start-up.

    Hi I have been getting the same error message on start up for some time. (Adobe Air is damaged). I have tried uninstalling, re-installing but still get the message and on certain websites I get 'missing plug-in' messages (usually involving playback o

  • Updating missing thumbnails in the PSE Organizer not working?

    I am using PSE12 as a test intallation. After editing photos (JPEG or RAW) with the PSE-Editor, those thumbnails are not visible anymore. Also there are 2 out of about twenty folders that do not show thumbnails at all. Updating the thumbnails doesn't

  • HT4993 Whats up with the new update?

    Why is everyone not being able to use imessage and faxetime

  • Expenses document problem in service calls

    Hi, In the service call document we have the expenses tab under which we can view all the documents (Inventory transfers,deliveries..etc) related to that call. Well now my problem is as follows... I create a service call and later i add many expenses

  • I'm running CS4 and use a Canon T3i to shoot RAW. Which camera raw patch do I need?

    I'm running CS4 and use a Canon T3i to shoot RAW. Which camera raw patch do I need? I get his error when trying to install the latest patch: "Adobe Application Manager is needed to resolve this problem, however it is missing or damaged. Download a ne