Exception in Java Uithreadingviolationexception

Asked

Viewed 62 times

2

I did a project with the LookAndFeel Substance, in it I have a thread which periodically updates a JProgressBar, the problem is that in function JProgressBar.setValue(1) and JProgressBar.setMaximum(10) occurs the exception below:

org.pushingpixels.substance.api.UiThreadingViolationException: Component state change must be done on Event Dispatch Thread
    at org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities.testComponentStateChangeThreadingViolation(SubstanceCoreUtilities.java:2072)
    at org.pushingpixels.substance.internal.ui.SubstanceProgressBarUI$SubstanceChangeListener.stateChanged(SubstanceProgressBarUI.java:87)
    at javax.swing.JProgressBar.fireStateChanged(JProgressBar.java:729)
    at javax.swing.JProgressBar$ModelListener.stateChanged(JProgressBar.java:652)
    at javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:364)
    at javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:302)
    at javax.swing.DefaultBoundedRangeModel.setMaximum(DefaultBoundedRangeModel.java:219)
    at javax.swing.JProgressBar.setMaximum(JProgressBar.java:898)

I want to know how I can fix this problem, or maybe how suppress it, as it does not impact on my layout.

  • http://stackoverflow.com/questions/19695247/substance-lf-not-working

  • @rrnan on that question he says he corrected by putting on invokeLater(), but how do I do it, it seems that this method is to build the thread that runs the application only I have another to update the component so I don’t know what it should look like

  • Related: http://answall.com/questions/2017/apply%C3%A7%C3%a3o-swing-by-o-m%C3%a9all-main-should-dispatch-to-create%C3%A7%C3%a3o-da-Gui-to-a-ed? Rq=1

1 answer

3


The Victor gave me the tip in the comments and I went after the link that he passed on to me, after delving into research I managed to understand the problem with this other link.

Below example of the class I made to update my JProgressBar. Notice that the magic is in SwingUtilities.invokeLater.

class AtualizaComponente implements Runnable {
    @Override
    public void run() {
        while (true) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    if (jProgressBar1.getValue() < jProgressBar1.getMaximum()) {
                        jProgressBar1.setValue(jProgressBar1.getValue() + 1);
                    } else {
                        jProgressBar1.setValue(0);
                    }
                }
            });
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.