2

我想使用JOptionPanes 处理一些异常。这是主要方法:

public class MainRun {

public static void main(String args[]){    
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(new SubstanceRavenGraphiteGlassLookAndFeel());
            }catch (Exception e){
                e.printStackTrace();
            }                 
            new MainGui().setVisible(true);                
            new Initialized().start();
            new PriorityMessageQueue().start();
        }
    });

MainGui是应用程序的主窗口 ( JFrame)。PriorityMessageQueue我在线程内处理了异常。

public class PriorityMessageQueue extends Thread {

@Override
public void run() {

    while (true) {
        try {
            instantMessages = instant.getMobitelMessagesToBeSent();
        } catch (Exception ex) {
                        JOptionPane.showMessageDialog(
                        null,
                        ex.getMessage(),
                        "Database Error",
                        JOptionPane.ERROR_MESSAGE);

        }

        ...

运行此程序后,出现错误

   org.jvnet.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
at org.jvnet.substance.utils.SubstanceCoreUtilities.testComponentCreationThreadingViolation(SubstanceCoreUtilities.java:2312)
at org.jvnet.substance.SubstanceOptionPaneUI.createUI(SubstanceOptionPaneUI.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:37)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:244)
at javax.swing.UIDefaults.getUI(UIDefaults.java:752)
at javax.swing.UIManager.getUI(UIManager.java:989)
at javax.swing.JOptionPane.updateUI(JOptionPane.java:1859)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1822)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1785)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1753)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1731)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1711)
at GUI.MainGui.showOptionPane(MainGui.java:1039)
at SMS.PriorityMessageQueue.run(PriorityMessageQueue.java:86)
    UIDefaults.getUI() failed: createUI() failed for
      javax.swing.JOptionPane
   [,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,p
   referredSize=,icon=,initialValue=,message=DatabaseConnection class
   connect,messageType=ERROR_MESSAGE,optionType=DEFAULT_OPTION,wantsInput=false]
   java.lang.reflect.InvocationTargetException
   java.lang.Error
at javax.swing.UIDefaults.getUIError(UIDefaults.java:712)
at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
at javax.swing.UIDefaults.getUI(UIDefaults.java:758)
at javax.swing.UIManager.getUI(UIManager.java:989)
at javax.swing.JOptionPane.updateUI(JOptionPane.java:1859)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1822)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1785)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1753)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1731)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1711)
at GUI.MainGui.showOptionPane(MainGui.java:1039)
at SMS.PriorityMessageQueue.run(PriorityMessageQueue.java:86)

并且JOptionPane将无法正常显示!

在此处输入图像描述

我怎样才能获得 parentComponent ( ),而不是传递null给?JOptionPaneMainGui JFrame

4

2 回答 2

6

A JOptionPane is a mini Swing GUI, and like all Swing GUIs, it must be created on the Swing event thread, not on a background thread. This can be done by creating your JOptionPane in a Runnable and then queuing the Runnable on the event thread:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    JOptionPane.showMessageDialog(....);
  }
});

For example,

} catch (Exception ex) {
   final String exMessage = ex.getMessage();

   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       JOptionPane.showMessageDialog(
         null,
         exMessage,
         "Database Error",
         JOptionPane.ERROR_MESSAGE);
       );
     }
   });
  }
}

As for your question,

Instead of passing null to JOptionPane, how can I get the parentComponent (MainGui JFrame)?

You'll need a reference to the main GUI. How you get this will depend on how your program is organized.

于 2012-08-29T21:38:11.963 回答
3
  • SubstanceEDT 非常敏感,所有更新都必须在 EDT 上完成

  • 否则你只能从ts中看到toolbar(来自Native OSSwing JComponen

  • 或例外RepaintManager(我认为来自Trident包装)

  • JOptionPane必须包裹在invokeLater()中,然后你的JOptionPanecoudl 是可见的,

  • 我认为你exception在谈论另一个问题,隐藏在你的代码中的东西,也许没有什么东西 used Substance L&F,这是次要问题

于 2012-08-29T22:33:06.407 回答