Cómo generar excepciones desde RepaintManager
En relación con mi pregunta ( puede ser ), encontré otro tipo de excepción que no pude detectar e imprimir desde SwingWorker
el hilo.
¿ Cómo puedo generar RepaintManager
excepciones?
Leí esto CheckThreadViolationRepaintManager
y este enfoque de Alexander Potochkin
, pero nada parece resolver mis problemas.
Aceptado
Si ayuda, el siguiente ejemplo imprime múltiples variaciones de lo siguiente Exception
, principalmente para cada fase de la inicialización del delegado de UI del marco. Usé CheckThreadViolationRepaintManager
, pero la variación también parece interesante.AspectJ
java.lang.Excepción en EDTViolation$CheckThreadViolationRepaintManager.checkThreadViolations(EDTViolation.java:43) en EDTViolation$CheckThreadViolationRepaintManager.addDirtyRegion(EDTViolation.java:37) en javax.swing.JComponent.repaint(JComponent.java:4734) en java.awt.Component.repaint(Component.java:3168) en javax.swing.JComponent.setFont(JComponent.java:2727) en javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:191) en javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:49) en javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:39) en javax.swing.JComponent.setUI(JComponent.java:662) en javax.swing.JPanel.setUI(JPanel.java:136) en javax.swing.JPanel.updateUI(JPanel.java:109) en javax.swing.JPanel.(JPanel.java:69) en javax.swing.JPanel.(JPanel.java:92) en javax.swing.JPanel.(JPanel.java:100) en javax.swing.JRootPane.createGlassPane(JRootPane.java:528) en javax.swing.JRootPane.(JRootPane.java:348) en javax.swing.JFrame.createRootPane(JFrame.java:255) en javax.swing.JFrame.frameInit(JFrame.java:236) en javax.swing.JFrame.(JFrame.java:159) en EDTViolation.main (EDTViolation.java:12) ...
import java.lang.ref.WeakReference;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.RepaintManager;
import javax.swing.SwingUtilities;
/** @see https://stackoverflow.com/questions/7787998 */
public class EDTViolation {
public static void main(String args[]) {
RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private static class CheckThreadViolationRepaintManager extends RepaintManager {
//http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
private boolean completeCheck = true;
private WeakReference<JComponent> lastComponent;
public CheckThreadViolationRepaintManager(boolean completeCheck) {
this.completeCheck = completeCheck;
}
public CheckThreadViolationRepaintManager() {
this(true);
}
public boolean isCompleteCheck() {
return completeCheck;
}
public void setCompleteCheck(boolean completeCheck) {
this.completeCheck = completeCheck;
}
@Override
public synchronized void addInvalidComponent(JComponent component) {
checkThreadViolations(component);
super.addInvalidComponent(component);
}
@Override
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())) {
boolean repaint = false;
boolean fromSwing = false;
boolean imageUpdate = false;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (StackTraceElement st : stackTrace) {
if (repaint && st.getClassName().startsWith("javax.swing.")
&& // for details see
// https://swinghelper.dev.java.net/issues/show_bug.cgi?id=1
!st.getClassName().startsWith("javax.swing.SwingWorker")) {
fromSwing = true;
}
if (repaint && "imageUpdate".equals(st.getMethodName())) {
imageUpdate = true;
}
if ("repaint".equals(st.getMethodName())) {
repaint = true;
fromSwing = false;
}
if ("read".equals(st.getMethodName()) && "javax.swing.JEditorPane".equals(st.getClassName())) {
// Swing reads html from a background thread
return;
}
}
if (imageUpdate) {
//assuming it is java.awt.image.ImageObserver.imageUpdate(...)
//image was asynchronously updated, that's ok
return;
}
if (repaint && !fromSwing) {
//no problems here, since repaint() is thread safe
return;
}
//ignore the last processed component
if (lastComponent != null && c == lastComponent.get()) {
return;
}
lastComponent = new WeakReference<JComponent>(c);
violationFound(c, stackTrace);
}
}
protected void violationFound(JComponent c, StackTraceElement[] stackTrace) {
System.out.println();
System.out.println("EDT violation detected");
System.out.println(c);
for (StackTraceElement st : stackTrace) {
System.out.println("\tat " + st);
}
}
}
}
Crea tu propia excepción
class RepaintManagerException extends Exception
{
String msg;
RepaintManagerException()
{
msg = new String("type message here");
}
}
Uso
public class My_Exception
{
public static void main (String args [ ])
{
try
{
// your code
if (expression) throw new RepaintManagerException( );
}
catch (RepaintManagerException e)
{
System.out.println (e);
}
}
}