show multiple MessageDialog at a time

只愿长相守 提交于 2019-12-20 02:47:10

问题


I am trying to make a program that displays 3 MessageDialog boxes at a time. I thought that if you put JOPtionPane.showMessageDialog in an actionListner class for a swing timer, it would show a new MessageDialog box every second.

So here is the code that I came up with:

package pracatice;

import java.awt.event.*;

import javax.swing.*;

public class practice extends JFrame 
{
    public static int num = 0;
    public static TimerClass tc = new TimerClass();
    public static Timer timer = new Timer(1000, tc);
    public JPanel panel = new JPanel();
    public JButton btn = new JButton("press");

    public practice()
   {
        setSize(100,100);
        setTitle("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setPanel();
        setVisible(true);   
  }
     public void setPanel()
     {
        btn.addActionListener(new listener());
        panel.add(btn);

        add(panel);
    }

    public class listener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            num = 0;
            System.out.println("starting timer");
            timer.start();
        }
    }

      public static class TimerClass implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Adding 1 to num");
            num++;
            JOptionPane.showMessageDialog(null,"Test");
           if(num == 3)
           {
                System.out.println("stopping the timer");
                timer.stop();
            }
       }
 }  

 public static void main(String[] args)
 {
    practice p = new practice();
    System.out.println("created an instance of practice");
 }

}

It works, but not the way I want it to. Instead of showing a new box every second it shows a new one 1 second after you press ok on the previous one.

So when I press "press" it waits 1 second and spawns the box. When I press "ok", it waits 1 second and spawns another one and so on. Any idea how to make the 3 boxes spawn 1 after another?


回答1:


When using the showX methods of JOptionPane you are creating modal (blocking and one at a time) dialogues as stated by the documentation. You can Use the JOptionPane directly by creating it manually instead of using the showX methods.

create a new one manually and set it to not be modal:

optionPane = new JOptionPane(...);
dialog = optionPane.createDialog(null, "the title");
dialog.setModal(false);
dialog.show();



回答2:


The methods to create the dialogs (JOptionPane.show...) do not return until the user has somehow closed the dialogs. Given Swing is single threaded, no other Swing process can happen until this occurs. If you wish to have three dialogs open at once, use a non-modal dialog.



来源:https://stackoverflow.com/questions/38273957/show-multiple-messagedialog-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!