7

I have 2 swing classes which extends JFrame. Both have show() method in there constructor. From ClassOne i called ClassTwo like new ClassTwo() on button click event. But if i press the button again new window for ClassTwo is opened. So how can i stop opening of ClassTwo window if one ClassTwo window is opened ?

Edit

now this problem is solved but now when i first open ClassTwo window it shows one window. Then after closing it when i again open ClassTwo window it opens two window and this count keep on increamenting. Why this is happening?

EDIT 2

I found that its not swing problem but its problem from MultiUsreChat class of Samck API. So anyone who have worked on it help me.

the code in ClassOne is:

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

and in ClassTwo is:

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 of 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);
       }
     }
  });
}
Harry Joy
  • 58,650
  • 30
  • 162
  • 207

4 Answers4

4

Don't make the frame visible in the ClassTwo constructor. Instead, keep a reference to classTwo in classOne, and when the button is clicked, make it visible, like so:

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

Also change classTwo's default close operation to hide on close, instead of exit:

setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

So it doesn't matter how many times the button is clicked, because all it does is make the existing instance visible. It doesn't create new instances.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • this problem is solved but now when i first open ClassTwo window it shows one window. Then after closing it when i again open ClassTwo window it opens two window and this count keep on increamenting. Why this is happening? – Harry Joy Feb 02 '11 at 13:01
  • Your Edit#2 does not explain how you open the window. I believe you are still creating new instances of the ClassTwo window somewhere. Only create a single instance in the constructor of ClassOne, and then set its visibility in the button. +1 to dogbane. – Kajetan Abt Feb 09 '11 at 13:01
  • @Kdansky: i added complete code portion in which i call ClassTwo. and the of ClassTwo which opens previously closed windows. But if you wish i can share my complete code. ??? – Harry Joy Feb 11 '11 at 08:52
  • Missing from the source code: The button handler and the constructor of ClassOne. Which are the two portions that are of interest. – Kajetan Abt Feb 11 '11 at 13:49
  • @Kdansky: i've updated my question with constructor source of classone. Take a look. `reply me using @Harry so i can get your reply even if i'm not in this question.` – Harry Joy Feb 12 '11 at 04:49
3

In ClassOne you could simply remember whether you opened a new ClassTwo using a boolean.

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

You should also hook into the dispose event of class two to reset the classTwoShown flag.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
2

try using the singleton pattern

yahh
  • 1,175
  • 3
  • 14
  • 41
0

The reason it does not work is that you are creating a new instance of ClassTwo inside the button handler, which means you create a new window every time the button is pressed. 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 the other answers inside the button handler, such as toggle functionality or other complex ideas, such as a singleton. But the main thing is that you should note create a new window in the button handler, but create it somewhere where it only gets called exactly once.

Edited for multiple windows and buttons.

Kajetan Abt
  • 1,505
  • 3
  • 15
  • 28
  • @Kdansky: This solution is not useful for me cause I want multiple ClassTwo windows open at same time. So I have to create new instance every time I click on button. – Harry Joy Feb 14 '11 at 14:38
  • You wrote "So how can i stop opening of ClassTwo window if one ClassTwo window is opened ?". Please clarify. – Kajetan Abt Feb 14 '11 at 14:40
  • @Kdansky: It was my question which was solved but the next question is it is opening previously closed windows. see my `Edit`. – Harry Joy Feb 14 '11 at 14:46
  • @Harry: See my edit. Change the constructor of classtwo so the window will never be destroyed, but only hidden. – Kajetan Abt Feb 14 '11 at 14:50
  • @Kdansky: +1, your solution solves problem if there is only one button nut in my case there are dynamic number of buttons. So if I do like this then I also have to maintain list or map of opened window which are hidden. So do you have any idea about how can I do it? – Harry Joy Feb 14 '11 at 14:57
  • I would recommend using a HashMap instead of a single variable. That way, you always easily know which window belongs to which button. – Kajetan Abt Feb 14 '11 at 15:02
  • @Kdansky: thnx for your help and time. – Harry Joy Feb 14 '11 at 15:10
  • Next time you should just paste more code, as all other replies were identical to mine. ;) – Kajetan Abt Feb 14 '11 at 15:13