JOptionPane给出错误

时间:2012-08-29 21:32:53

标签: java swing joptionpane event-dispatching substance

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

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无法正常显示!

enter image description here

我如何获取parentComponent(null JOptionPane)而不是将MainGui传递给JFrame

2 个答案:

答案 0 :(得分:6)

JOptionPane是一个迷你Swing GUI,与所有Swing GUI一样,它必须在Swing事件线程上创建,而不是在后台线程上创建。这可以通过在JOptionPane中创建Runnable然后在事件线程中排队Runnable来完成:

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

例如,

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

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

关于你的问题,

  

我如何获取parentComponent(null JOptionPane)而不是将MainGui传递给JFrame

您需要对主GUI的引用。你如何得到这将取决于你的计划的组织方式。

答案 1 :(得分:3)

  • Substance对EDT非常敏感,所有更新都必须在EDT上完成

  • 否则您只能从toolbar ts

  • 中看到Native OS(来自Swing JComponen) 来自RepaintManager
  • 或例外(我认为来自Trident包)

  • JOptionPane必须包含在invokeLater()中,然后您的JOptionPane coudl可见,

  • 我认为您的exception正在讨论另一个问题,即您的代码中隐藏的内容,可能没有使用Substance L&F的内容,这是次要问题