Creating multiple instance of an object with same variable in Java

风格不统一 提交于 2019-12-24 14:34:54

问题


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.


回答1:


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.




回答2:


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();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/5528408/creating-multiple-instance-of-an-object-with-same-variable-in-java

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