1

I have a JFrame with a Button that opens a different JFrame. But I want the button to only open the second frame once. Problem is, every time I click it I get a new instance of the frame. This must be a very common problem, since I'm following a book on how to create this GUI. I find it odd that the author didn't mention this "feature".

So how do I keep my button from opening multiple copies of the new frame?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MBFROMIT
  • 13
  • 1
  • 5

4 Answers4

2

You should keep a reference to the sub frame you open for first time. At second time you first check if you have a reference or not and then decide to create a new frame or to put the focus onto the existing open frame.

Example as answer to comment of OP (similar to other answer of @AlexanderTorstling, but not immediately creating the sub frame):

class MainFrame extends JFrame {
  private JFrame subFrame = null;

  MainFrame() {
    ...
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         if (subFrame == null) {
           subFrame = new JFrame();
           ...
         }
         subFrame.setVisible(true);
      }
    });    
  }
}

This example has also the advantage to give you the possibility to close the subframe via a registered WindowAdapter if the main frame is closed.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • This is how I would have tried to do it, but how? How do you "check if you have a reference or not"? Code example? Thanks! – MBFROMIT Jan 04 '14 at 22:43
2

Instead of letting the button create a new JFrame every time, make the second JFrame a member of the first JFrame, and only let the button call jframe2.setVisible(true);:

class JFrame1 {
   JFrame2 jframe2=...;
   JButton button=...;

   JFrame1() {
     ...
     button.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         jframe2.setVisible(true);
       }
     });
     ...
   }
}
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
2

UPDATED!

try this:

JFrame frame2 = new JFrame(); // instance variable

...

//when button is clicked
button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if(!frame2.isVisible())
            frame2.setVisible(true);
    }
});

make sure you are handling the closing of all of the JFrames manually like this:

frame2.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        // handle closing the window
        frame2.setVisible(false);
        frame2.dispose();
    }
});

instead of using the JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hope this helps.

doomsdaymachine
  • 647
  • 6
  • 11
0

Please try this code snippet:

JFrame frame2 = new JFrame(); // instance variable
boolean secondWindowIsOpne = false;

//when button is clicked
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
    if(secondWindowIsOpne == false){
        frame2.setVisible(true);
        secondWindowIsOpne  = true;
    }
    else{
      System.out.println("This Window is already running");
   
  }
   });

Make sure you are handling the closing of all of the JFrames manually like this:

frame2.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
    // handle closing the window
      secondWindowIsOpne = false;
    frame2.setVisible(false);
    frame2.dispose();
}

 });
Spindizzy
  • 7,244
  • 1
  • 19
  • 33
Sandeep
  • 24
  • 1