1

I have two radio buttons in frame1. On click on enable radio button, it will popup another frame called frame2. I want, not to close the frame1 while the frame2 is opened. But it get closed when click on the X. I used "frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);". Still it get closed.

enable.addItemListener(new ItemListener() 
{
  @Override
  public void itemStateChanged(ItemEvent e) 
   {
     // TODO Auto-generated method stub                     
    frame2.setVisible(true);
    frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }
Manikandan
  • 1,479
  • 6
  • 48
  • 89

3 Answers3

3

There are number of methods to get the list of active window instances and verify which frame/window is visible or not.

  1. Window.getOwnedWindows()
  2. Window.getWindows()
  3. Window.getOwnerlessWindows()
  4. Frame.getFrames()
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

In case you need to work with the frame2 only, you may try to use dialogs. A brief googling discovered another solution as well.

wanderlust
  • 1,826
  • 1
  • 21
  • 25
0

Have a look here: How can a Swing WindowListener veto JFrame closing

What you will need to do is, in frame1 and frame2 you will need to set setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE). Then in the below code:

frame1.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
       //check is frame 2 is open.. if it is then return without doing anything, else
       // frame1.dispose();
    }
});
Community
  • 1
  • 1
sethu
  • 8,181
  • 7
  • 39
  • 65