vikky 2405
vikky 2405

Reputation: 289

Erron using invokeAndWait in Java Swing

I have created a Swing GUI which is called from an other Java class file.

constructor of the GUI:

public AdviceGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {

    System.out.println("I am called");

    //AdviceGUI.model= model;
    initComponents();

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
                .getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(AdviceGUI.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);
    }

    //check
    java.awt.EventQueue.invokeAndWait(new Runnable() {
        public void run() {
            try {
                new AdviceGUI(model).setVisible(true);
            } catch (InvocationTargetException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

}

Function Calling the GUI's constructor in the main Java class:

public Map<String,ArrayList<String>> queryExpertGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {
    for(String str : model.orderedQueries) {
        Utils.println("Clause:" + str);
    }

    //Modes file
    //String mode_dir =  cmdArgs.getTrainDirVal()
    System.out.println("sssssssssss"+" "+model.convertModelForBK());

    model.setUserClauses(model.convertModelForBK());
    System.out.println("ssss"+" "+model.userClauses);

    AdviceGUI a = new AdviceGUI(model);

Use case is call the GUI in for loop every time the function calling the constructor of the GUI is called and wait until the GUI(Jframe) closes after clicking a button in GUI. To accomplish this I have used the invokeAndWait() in the constructor. But I get the following error:

Caused by: java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1303)
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1296)
at edu.wisc.cs.will.Boosting.UI.AdviceGUI.<init>(AdviceGUI.java:87)
at edu.wisc.cs.will.Boosting.UI.AdviceGUI$1.run(AdviceGUI.java:90)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

New to Java, any suggestion would be helpful.

Upvotes: 0

Views: 707

Answers (1)

AJNeufeld
AJNeufeld

Reputation: 8695

Looks like you have unintentional recursion going on here:

public AdviceGUI(AdviceModel model) throws InvocationTargetException, InterruptedException {
    // java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { try {
    new AdviceGUI(model).setVisible(true);
    // } catch (InvocationTargetException | InterruptedException e) { } } } });
}

The AdviceGUI constructor will attempt to construct another AdviceGUI instance, which will create another, which will create another ...

You should revisit who is creating the first AdviceGUI instance, and at that point, use SwingUtilities.invokeLater( new Runnable() { ... } ) to construct and show the UI, and remove the invokeLater from the AdviceGUI constructor.

Upvotes: 1

Related Questions