Java: JMenuItem throws exception when clicked: IllegalComponentStateException

184 views Asked by At

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!

0

There are 0 answers