tags:

views:

36

answers:

3

Hello experts,

I work on a UI java application composed from:

  • top header
  • left menu
  • header text
  • error message (displayed on the page's header)
  • center container (JPanel) which is changed dinamically, in dependence of selected menu item (from left side)

When there are any computation errors, an error message is displayed on the application's header (location in the page's header).Error messages can be different, ones are composed from one row, but others are longer (2,3 rows)

When a long error appears, it may be displayed correctly or not (the message is displayed over other components). As far as I know, on changing UI components the doLayout(), repaint() methods are invoked indepenendly and these should correct the layout.

Recently I read about EDT and noticed that a swing application should be organised according to EDT principles.

I think that this will solve my problem, because all the operations will be synchronized and I'll manage the method calls' consecutivity.

But now, I don't have enough time to refactor the application and this is scheduled for next release.

Do you know another solution which will help in adjusting layouts? This problem is very tricky, I'm not able to reproduce it. But it appears from time to time.

Thanks a lot. Sergiu

PS: If you have any questions or more details are needed, please don't hesitate to contact me.

A: 

Are you sure that all the UI code is executed in EDT? If not, try setting just for a sake of testing Substance Look&Feel. It's throwing an exception in case of execution of UI code in a different thread.

Boris Pavlović
Thank you Boris! I'm sure that my UI code doesn't follow all the EDT standards, but as I said earlier, I'll adjust all the code on next release. Actually, I'm looking for a workaround, not too time-consuming.
Sergiu Vazdautan
A: 

If you have to manipulate Swing components' state and you aren't certain that your code is running in the EDT, SwingUtilities.invokeAndWait() and SwingUtilities.invokeLater() may be of help.

perp
Thank you for your response. Basically, I use the methods specified above, but I'll be able to use them for entire application such on next release.
Sergiu Vazdautan
A: 

The following class checks for EDT violations. Install it before doing any other GUI related code as follows:

RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());

Here's the class:

import javax.swing.JComponent;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;

public class CheckThreadViolationRepaintManager extends RepaintManager {
    private boolean completeCheck = true;

    public synchronized void addInvalidComponent(JComponent component) {
        checkThreadViolations(component);
        super.addInvalidComponent(component);
    }

    public boolean isCompleteCheck() {
        return completeCheck;
    }

    public void setCompleteCheck(boolean completeCheck) {
        this.completeCheck = completeCheck;
    }

    public void addDirtyRegion(JComponent component, int x, int y, int w, int h) {
        checkThreadViolations(component);
        super.addDirtyRegion(component, x, y, w, h);
    }

    private void checkThreadViolations(JComponent c) {
        if (!SwingUtilities.isEventDispatchThread() && (completeCheck || c.isShowing())) {
            Exception exception = new Exception("EDT Violation");
            boolean repaint = false;
            boolean fromSwing = false;
            StackTraceElement[] stackTrace = exception.getStackTrace();
            for (StackTraceElement st : stackTrace) {
                if (repaint && st.getClassName().startsWith("javax.swing.")) {
                    fromSwing = true;
                }
                if ("repaint".equals(st.getMethodName())) {
                    repaint = true;
                }
            }
            if (repaint && !fromSwing) {
                //no problems here, since repaint() is thread safe
                return;
            }
            exception.printStackTrace();
        }
    }
}
yrn1
Thank's a lot for your response. Your code helped me to find EDT Violations which I plan to fix them further. Best regards.
Sergiu Vazdautan