eros
eros

Reputation: 103

Hide/Simply close Jdialog on click of X

I am trying to set the default close operation on close of Jdialog. However, my progressbar code is overriding the closing event. I wasn't sure of a place to write the

dialog.setVisible(true); and  dialog.setDefaultCloseOperation(HIDE_ON_CLOSE); 

statements.

Below is my init() method and and my Jdialog. Please review and suggest.

  @Override
        public void init() {

            dialog = new JDialog(new JFrame(), "Progress", true);
            dialog.setSize(300, 75);
            dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
            }

        public void updateProgressBar(final int progress) {
        dialog.setAlwaysOnTop(true);
        for (i = 0; i <= 100; i = i + 1) {

         SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
         progressBar.setValue(progress);
         dialog.setVisible(true);
        }
        });
    }
    if(progressBar.getValue()==100){
    dialog.dispose();
    System.out.println("File uploaded successfully!");
}

What is not working here is - the dialog.setDefaultCloseOperation(). Why is it not working is - because I have the dialog.setVisible(true) in my updateProgessBar() method, to show the updating progress of my fileupload.

Each time there is a progress, the dialog.setVisible becomes true, thereyby overriding the setDefaultCloseOperation written in the init() method. If I place the dialog.setVisible(true) present in my updateProgressBar() elsewhere, the jdialog progress bar would not update and appear as expected.

So, my question is how would I adjust these two statements within the code, so that, I will also be able to perform the default close operation ?

Upvotes: 1

Views: 805

Answers (2)

das_j
das_j

Reputation: 4794

To handle the klick of the 'X' (as well as pressing Alt+F4 on Windows or closing the window in the Task Manager), you can add a WindowListener: dialog.addWindowListener(...);. Because WindowListener is an interface with a lot of methods you might not need, you can also use a WindowAdapter.

Instead of explainging, I will show you two samples:

dialog.addWindowListener(new WindowListener() {
    @Override
    public void windowOpened(WindowEvent arg0) {
    }

    @Override
    public void windowIconified(WindowEvent arg0) {
    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
    }

    @Override
    public void windowClosing(WindowEvent arg0) {
        //User clicked 'X'
    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        //Window is closed, now you can free resources if you need.
    }

    @Override
    public void windowActivated(WindowEvent arg0) {
    }
});

Second sample:

dialog.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent arg0) {
        //User clicked 'X'
    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        //Window is closed, now you can free resources if you need.
    }
});

As you can see, if you use WindowAdapter, you can just pick the methods that you need.

Upvotes: 0

das_j
das_j

Reputation: 4794

It looks like you want to display a JDialog with a Progress bar in it. Swing has an own class to do that, ProgressMonitor. Screenshot:

Screenshot of ProgressMonitor

If that is what you need, Javadoc is here, sample/tutorial is here.

Upvotes: 3

Related Questions