2011-06-30 29 views

Respuesta

8

Usted puede crear un JOptionPane manualmente, sin métodos estáticos:

JOptionPane pane = new JOptionPane("Your message", JOptionPane.INFORMATION_MESSAGE); 
JDialog dialog = pane.createDialog(parent, "Title"); 

entonces usted puede mostrar el diálogo y el fuego de un temporizador para ocultarlo después de diez segundos.

0

hacen pequeño marco como JOptionPane, muestran en hilo y deseche después de 10 seg

2

Mi Java es un poco oxidado, pero usted debería ser capaz de usar sólo el estándar de clase Timer:

import java.util.Timer; 

int timeout_ms = 10000;//10 * 1000 
Timer timer = new Timer(); 
timer.schedule(new CloseDialogTask(), timeout_ms); 

//implement your CloseDialogTask: 

class CloseDialogTask extends TimerTask { 
    public void run() { 
    //close dialog code goes here 
    } 
} 
+5

con swing, es preferible utilizar javax.swing.Timer en lugar de éste. –

6

Intenté estas respuestas y me encontré con el problema de que mostrar el diálogo es una llamada de bloqueo, por lo que un temporizador no puede funcionar. Lo siguiente evita este problema.

 JOptionPane opt = new JOptionPane("Application already running", JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}); // no buttons 
    final JDialog dlg = opt.createDialog("Error"); 
    new Thread(new Runnable() 
     { 
      public void run() 
      { 
      try 
      { 
       Thread.sleep(10000); 
       dlg.dispose(); 

      } 
      catch (Throwable th) 
      { 
       tracea("setValidComboIndex(): error :\n" 
        + cThrowable.getStackTrace(th)); 
      } 
      } 
     }).start(); 
    dlg.setVisible(true); 
1
// ===================== 
// yes = 0, no = 1, cancel = 2 
// timer = uninitializedValue, [x] = null 

public static void DialogBox() { 

    JOptionPane MsgBox = new JOptionPane("Continue?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION); 
    JDialog dlg = MsgBox.createDialog("Select Yes or No"); 
    dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    dlg.addComponentListener(new ComponentAdapter() { 
     @Override 
     // ===================== 
     public void componentShown(ComponentEvent e) { 
     // ===================== 
     super.componentShown(e); 
     Timer t; t = new Timer(7000, new ActionListener() { 
      @Override 
      // ===================== 
      public void actionPerformed(ActionEvent e) { 
      // ===================== 
      dlg.setVisible(false); 
      } 
     }); 
     t.setRepeats(false); 
     t.start(); 
     } 
    }); 
    dlg.setVisible(true); 
    Object n = MsgBox.getValue(); 
    System.out.println(n); 
    System.out.println("Finished"); 
    dlg.dispose(); 
    } 
} 
+1

Esto podría resolver el problema, pero debería explicar cómo y por qué esto lo resuelve, no solo el código. – Adam

Cuestiones relacionadas