0

So, I've been looking a while for a solution to my problem, but it just doesn't work. Here is the piece of code that is giving me problems:

else if(xy==true || xz==true)
    {
        mm1.setVisible(true);
        mm2.setVisible(true);
        mm1.repaint();
        mm2.repaint();
        SwingUtilities.updateComponentTreeUI(this);
        this.validateTree();
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
        sound.play();
        sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
        sound1.play();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) {
            Logger.getLogger(formulario2.class.getName()).log(Level.SEVERE, null, ex);
        }
        formulariogame over2=new formulariogame();
        over2.setVisible(true);
        this.dispose();
    }

I've tried using the "synchronized" method, using repaint(), changing the order, but I keep getting this:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This function should be called while holding treeLock
at java.awt.Component.checkTreeLock(Component.java:1196)
at java.awt.Container.validateTree(Container.java:1682)
at nivel_2.formulario2.AceptarActionPerformed(formulario2.java:148)
at nivel_2.formulario2.access$100(formulario2.java:20)
at nivel_2.formulario2$2.actionPerformed(formulario2.java:93)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I don't get any error at the source editor, and using any other method, the JFrame doesn't refresh. What can I do to make it work?

Pd.: the error appears just when this block of code executes.

  • 1
    Why do you need to call `SwingUtilities.updateComponentTreeUI(this);` and/or `this.validateTree()` at all? – MadProgrammer Aug 12 '14 at 00:12
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Aug 12 '14 at 07:23
  • Possible duplicate of [validateTree in Java 7.x doesnt work (in Java 6.x was fine)](http://stackoverflow.com/questions/9758069/validatetree-in-java-7-x-doesnt-work-in-java-6-x-was-fine) – Balder Jan 12 '16 at 06:41

2 Answers2

1

Do not use Thread.sleep() as it will freeze your Swing application.

Instead you should use a javax.swing.Timer.

See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
  • Ok, now I tried to use the timer: I imported the package, tried to create a new timer ( public Timer timer1=new Timer(4000, addActionListener(this)) ), but I get an error on the source editor. I don't really know how to create an ActionListener, so could you help me? – DMDemon Aug 12 '14 at 01:51
  • [How to Write an Action Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – DavidPostill Aug 12 '14 at 07:20
  • You should read the whole Swing tutorial, starting at [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/index.html) – DavidPostill Aug 12 '14 at 07:22
0

I think I haven't given enough informaton, or I'm too noob (I don't know), so I'm gonna put here all the class that is giving me problems (It's a Jbutton), changed with the recomendations you have given me (still not working):

private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {                                        

    if(xy==true && Respuesta.getText().equals("verdadero"))
    {
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/enter.wav"));
        sound.play();
        r1.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
        r2.setVisible(true);
        dialog.setText("<html>\n" +
                       "<font color='blue'><center><h4>¡BIEN HECHO!<br>ahora resuelve la siguiente<br>pista.</h4></center> </font>\n" +
                       "</html>");
        x=100;
        xy=false;
        xz=true;
    }
    else if(xz==true && Respuesta.getText().equals("falso"))
    {
        r2.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
        dialog.setText("<html>\n" +
                       "<font color='blue'><center><h4>¡GENIAL!<br>¡VAMOS A LA SIGUIENTE CÁMARA!</h4><h5>Haz click aquí</h5></center> </font>\n" +
                       "</html>");
        x=2;
    }
    else if(xy==true || xz==true)
    {
        mm1.setVisible(true);
        mm2.setVisible(true);
        mm1.repaint();
        mm2.repaint();
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
        sound.play();
        sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
        sound1.play();
        Timer timer1= new Timer(4000, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }   
        });
        timer1.start();
        formulariogame over2=new formulariogame();
        over2.setVisible(true);
        this.dispose();
    }

}                                       

What I need, is that when timer1.starts(), the execution stops for 4 seconds, giving time for the program to refresh the image. If Timer method doesn't work for what I want, please give me another recommendation. I would thank you a lot if you could send me the corrected code.