0

I'm trying to use a JMenuBar in my program, but every time the JMenuItem is clicked it throws an exception.

I've made a very simple code snippet to explain how I am currently using JMenuBar, JMenu and JMenuItem:

import javax.swing.*;

public class JMenuTest extends JFrame {
    JMenuTest() {
        super("Tester");

        JMenuBar tb = new JMenuBar();
        JMenu a = new JMenu("File");
        JMenuItem z = new JMenuItem("Öppna osv");   

        tb.add(a);
        a.add(z);

        setJMenuBar(tb);

        setSize(300, 300);
        setVisible(true);
    }   

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

And here's the stack trace:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1995)
    at java.awt.Component.getLocationOnScreen(Component.java:1969)
    at apple.awt.CAccessibility$22.call(CAccessibility.java:370)
    at apple.awt.CAccessibility$22.call(CAccessibility.java:368)
    at apple.awt.CToolkit$CallableWrapper.run(CToolkit.java:1125)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
    at apple.awt.CToolkit$CPeerEvent.dispatch(CToolkit.java:1215)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Thank you very much for any replies!

Georg Leber
  • 3,470
  • 5
  • 40
  • 63
Daniel Setréus
  • 552
  • 4
  • 16
  • I don't get an exception from your code. I can click the menu item as many times as I want, it works. – peter.petrov Dec 19 '13 at 12:01
  • your example works just fine for me – dehlen Dec 19 '13 at 12:01
  • 1
    Works fine for me too. Do you have more code? – Sergi Dec 19 '13 at 12:02
  • Is it possible you've missed some of the above steps in your real code? – Paul Samsotha Dec 19 '13 at 12:26
  • This is really weird. It will not work for me. Have tried with both Java 6 and Java 7. First I ran it through Eclipse - but I have also tried to compile and run manually through terminal. I guess you tried on windows machines? Starting to think it might be a osx thing? I noticed one of the lines in the exception tree says "apple.awt...." – Daniel Setréus Dec 19 '13 at 12:35
  • I think you have a similar problem like here http://stackoverflow.com/questions/13161251/mouselistener-is-giving-problems-with-show-method – FuryFart Dec 19 '13 at 12:35
  • I've read that post... Similar yes. But I can't really understand how it applies to my question tough. I guess I could make sure the menu understands that it should use the surrounding JFrame to get the relative position. But how to do that? – Daniel Setréus Dec 19 '13 at 12:50
  • @OliverWatkins - what system? Did you get the exact same exception? – Daniel Setréus Dec 19 '13 at 12:51
  • are you using a modern version of Java? Im on java7 – Oliver Watkins Dec 19 '13 at 12:53
  • I'm starting to think it's a version issue as well. Turns out that when I compile and runt the code myself - in the terminal - everything works fine - and the version used is 1.7.0_45. However - when Eclipse runs the samt code it runts with 1.6.0_65. Can it really be that Sun changed something between 6 and 7 that applies here? I will read up on how to make Eclipse run with java 7 - and try that. – Daniel Setréus Dec 19 '13 at 13:11

0 Answers0