1

I have a modeless dialog which i need to show multiple instances of it displayed at the same time. I have kept the dialog as a member variable in a class which i new and show the dialog. Here there are multiple instances of dialog visible but i am assigning it to the same member variable.(I need to have it as member variable for some processing). It is working fine but i dont understand why this is working. Am i missig something very obvious?

public class ABC {
    CMyDialog m_dlg;

    onSomeEvent() {
       m_dlg = new CMyDialog();
    }
}

onSomeEvent is called multiple times and multiple dialogs are shown. Any idea how Java manages these things? Do i need to keep an array of CMyDialog as member variable instead of just a single class?

Any help is highly appreciated.

Thanks in advance. Nitin K.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nitin Kale
  • 45
  • 2
  • 4

2 Answers2

1

The default close operation for JDialog is HIDE_ON_CLOSE. If you don't want multiple dialogs, you can create just one and make it visible onSomeEvent(). This example uses a toggle button's itemStateChanged() handler.

import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5528408 */
public class DialogToggle extends JPanel {

    private static final String show = "Show Dialog";
    private static final String hide = "Hide Dialog";
    MyDialog myDialog = new MyDialog();

    public DialogToggle() {
        final JToggleButton b = new JToggleButton(show);
        b.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (b.isSelected()) {
                    myDialog.setVisible(true);
                    b.setText(hide);
                } else {
                    myDialog.setVisible(false);
                    b.setText(show);
                }
            }
        });
        this.add(b);
    }

    private class MyDialog extends JDialog {

        public MyDialog() {
            this.setLocationRelativeTo(DialogToggle.this);
            this.add(new JLabel("Hello, world!", JLabel.CENTER));
        }
    }

    private void display() {
        JFrame f = new JFrame("ABC");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogToggle().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks trashgod for your reply. What i am doing is, instead of creating the myDialog object in the declaration of class ABC, i am just declaring it as initial value null(CMyDialog myDialog = null;//new CMyDialog();) and in actionPerformed() handler i am simply creating new objects of CMyDialog, i.e. myDialog = new CMyDialog(); Now my question is, by clicking the button multiple times, it creates mulitple dialogs, but is this method good? or do I have to manage the instances of CMyDialog in Array or something like that? – Nitin Kale Apr 03 '11 at 09:44
  • Good or bad depends on the goal. Don't the extra dialogs clutter the screen? If you're closing them, you should probably do `setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)` in the dialog's constructor. – trashgod Apr 03 '11 at 09:57
0

Although there are several instances of dialog visible, each of it occupies a separate space in main memory. The variable name may be same, but all the dialog instances do not share same memory. I hope this is what you had asked for.

Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • Thanks Shankar for reply. So you mean I dont need to do anything special for managing this situation. By special, I mean to keep the array of opened dialogs and that stuff. Is it correct to assume that Java is managing it on its own? – Nitin Kale Apr 03 '11 at 08:23
  • 1
    @Nitin Kale, AFAIK you don't need to create any arrays in your case. Create array only if you want to use that reference for some purpose. If you just need the Dialog to be popped up and there are some UI events happening background, then you dont need to maintain a separate name for each reference. – Shankar Raju Apr 03 '11 at 08:34
  • 1
    I note that you say "I need to have it as member variable for some processing". Only the last created dialog will be available by the member variable, so it is likely your processing is broken on the other dialogs. – Simon G. Apr 03 '11 at 08:50
  • thanks Simon for your input. I will check whether it is giving any problems. – Nitin Kale Apr 03 '11 at 09:38