How to stop opening a duplicate window using the Smack API in swing?

I have 2 swing classes that extend with JFrame . Both have a show() method in the constructor. From ClassOne I called ClassTwo as new ClassTwo() on a button click event. But if I press the button again, a new window for ClassTwo will open. So, how can I stop a ClassTwo window from opening if one ClassTwo window is open?

Edit

Now this problem is being solved, but now, when I open the ClassTwo window, it shows one window. Then, closing it, when I open the ClassTwo window again, it opens two windows, and this count continues to increase. Why is this happening?

EDIT 2

I found that this is not a swing problem, but a problem with it from the Samck MultiUsreChat API class. Therefore, everyone who worked on this helps me.

code in ClassOne:

 if(!winList.contains(room_jid)){ new ClassTwo(room_jid,....); winList.add(room_jid); } 

and in ClassTwo:

 public ClassTwo(....){ ...... this.muc = new MultiUserChat(connection, room_jid); if(!muc.isJoined()) muc.join(this.user_id); //---- This line opens previously closed window. ..... if(!isVisible()) show(); } 

Edit 3

constructor classone

 public ClassOne(){ JButton btn = new JButton("Open"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(!winList.contains(room_jid)){ new ClassTwo(room_jid,....); winList.add(room_jid); } } }); } 
+7
source share
4 answers

The reason it doesn't work is because you create a new instance of ClassTwo inside the button handler, which means that you create a new window every time you click the button. This should work:

 private Map<JButton, ClassTwo> classTwoMap; public ClassOne(){ classTwoMap = new HashMap<JButton, ClassTwo>(); ClassTwo bn1window = new ClassTwo(); bn1window .setVisible(false); //initialisation code for your window ..... JButton btn = new JButton("Open"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ classTwoMap.Get(e.getSource()).setVisible(true); } }); classTwoMap.Get(btn).setvisible(false); } //Edit: public ClassTwo() { // This will hide the window when closed, and the button will re-"open" it. setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } 

You can use any combination of other answers inside the button handler, for example, to switch functions or other complex ideas such as singleton. But the main thing is that you should note the creation of a new window in the button handler, but create it somewhere where it will be called only once.

Edited for multiple windows and buttons.

0
source

Do not make the border visible in the ClassTwo constructor. Instead, save the classTwo reference in classOne, and when the button is clicked, make it visible, for example:

 //on button click if(classTwo == null){ classTwo = new ClassTwo(); } classTwo.setVisible(true); 

Also change the default close operation CloseTwo to hide when closing and not exit:

 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 

Thus, it does not matter how many times the button is pressed, because all this makes the existing instance visible. It does not create new instances.

+4
source

In ClassOne you can just remember if you opened a new ClassTwo with boolean .

 //in event handler for the button if (!classTwoShown) { classTwoShown = true; new ClassTwo(); } 

You must also attach the second class dispose event to the reset classTwoShown icon.

+3
source

try using singleton pattern

+2
source

All Articles