Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1418
  • Last Modified:

closing a JOptionPane

Hi,

maybe JOptionPane.createDialog is my answer, but...
I would like to display a message, and then once some threads are joined, programattically close the 'please wait' message box.
Is there a way to do this with JOptionPane?  Or must I make a custom JDialog?

Dan
0
dbrownell83
Asked:
dbrownell83
  • 5
  • 5
1 Solution
 
ashok3sepCommented:
Simply create a dialog (from the event dispatch thread) and show it,
when your processing is done then hide it.You don't need to worry about anything else.

EventQueue.invokeLater(new Runnable() { public void run()
{
   mydialog.show();
}});

// start your processing
0
 
ashok3sepCommented:
once you are done with the JOPtion Pane you can hide it by setVisible(false) or hide method.

you can also have it in a thread and ask it to wait for some seconds

Thread.sleep(5000);// (5 Seconds)
0
 
dbrownell83Author Commented:
my question was whether it is possible to do it with JOptionPane, or whether I must make a custom JDialog
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dbrownell83Author Commented:
a JOptionPane does not have a hide method, so I was thinking maybe the 'createDialog' method would give me a handle to a dialog, but then how would i set that dialog's content... (?)
0
 
ashok3sepCommented:
mport javax.swing.*;
import java.awt.event.*;


public class JDialogTest extends JDialog {
     
     public JDialogTest(){
                         
               JLabel nameLbl = new JLabel("Name:");
               JTextArea nameTxt = new JTextArea(10,1);
               
               JLabel passLbl = new JLabel("PassWord:");
               JTextArea passTxt = new JTextArea(10,1);
               
               JButton loginBtn = new JButton("Login");
               loginBtn.addActionListener(
                    new ActionListener(){
                         public void actionPerformed(ActionEvent e){
                             
                              System.out.println("Do something now...");
                              dispose();
                         }
                    }
               );
               
               JButton cancelBtn = new JButton("Cancel");
               cancelBtn.addActionListener(
                    new ActionListener(){
                         public void actionPerformed(ActionEvent e){
                             
                              dispose();
                         }
                    }
               );
               
               JPanel mainPanel = new JPanel();
               mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
               mainPanel.add(nameLbl);
               mainPanel.add(nameTxt);
               mainPanel.add(passLbl);
               mainPanel.add(passTxt);
         
               JPanel btnPanel = new JPanel();
               btnPanel.add(loginBtn);
               btnPanel.add(cancelBtn);
               
               mainPanel.add(btnPanel);
               
               getContentPane().add(mainPanel);
     }
     
     public static void main(String[] args){
         
          JDialogTest jdt = new JDialogTest();
          jdt.setSize(250,150);
          jdt.show();
     }
}
0
 
dbrownell83Author Commented:
this works:
 
                        final JOptionPane pane = new JOptionPane("PLEASE WAIT...",
                                JOptionPane.INFORMATION_MESSAGE);
                       
                        final JDialog dialog = new JDialog(mFrame, "Please wait...", true);
                        dialog.setContentPane(pane);
                       
                        dialog.setLocation(ODialerUIPreferences.getDataSourcePosition().x,
                                           ODialerUIPreferences.getDataSourcePosition().y);
                        dialog.pack();
                        dialog.setVisible(true);
                        dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));                                

...
(wait for thread joins)
...

                        dialog.setVisible(false);
                        dialog.setCursor(Cursor.getDefaultCursor());                      
0
 
dbrownell83Author Commented:
hmm, actually it doesnt...
0
 
ashok3sepCommented:
thanks for accepting
:-)
0
 
dbrownell83Author Commented:
i accepted because you reminded me I could just set the content pane of a JDialog to a JOptionPane.  

My code not working had to do with calling .setVisible() on a non-EDT thread, which led to blocking.  i think.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now