3

How do I exit a particular frame based on a simulation on a JPanel within that particular frame without exiting entire application?

In my main class I have a Frame() method

public void FightFrame(String offensemsg){      

    JFrame frame = new JFrame("BattleView: ");
    frame.setLayout(new BorderLayout());
    FightScene sc = new FightScene();       
    frame.add(sc);
    frame.setVisible(true);
    frame.setSize(652, 480);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    sc.GenerateScene(offensemsg);
}

in my FightScene class, I am drawing out a fightscene, the class also has checkCollision() method

      public void checkCollisions() {
           for (int i = 0; i < defense.size(); i++) {
                FriendlyEntity m = (FriendlyEntity) defense.get(i);    
                Rectangle r1 = m.getBounds();    
                for (int j = 0; j<offense.size(); j++) {
                    Enemy a = (Enemy) offense.get(j);
                    Rectangle r2 = a.getBounds();    
                    if (r1.intersects(r2)) {
                        m.setHealth(-1);
                        a.setHealth(-1);
                        if(a.getHealth()==0){
                        a.setVisible(false);
                        } else if(m.getHealth()==0){
                            m.setVisible(false);
                            }                       
                }}
           }               
           if(defense.size()==0){                   
                System.out.println("You have lost the battle\n");
                //############ How can I exit the FightFrame from here?
            }else if (offense.size()==0){
                System.out.println("You have won the battle\n");
            //############# How can I exit the FightFrame from here?                    
            }
        }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2556304
  • 159
  • 2
  • 15

2 Answers2

10

Set JFrame#setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE

JFrame frame=new JFrame();//create frame

//so when we exit or dispose of Jframe it doesnt exit the entire app
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

...

frame.pack();
frame.setVisible(true);

Now to close the frame simply do:

frame.dispose();//close the `JFrame` instance

Update:

I understand but how do I trigger this withing FightScene() (which is a JPanel)?

Pass instance of JFrame to JPanel via constructor or setter

or

if you dont want to use instances in the JPanel class/method do this:

JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
frame.dispose();
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • `if(defense.size()==0){ System.out.println("You have lost the battle\n"); //############ Force close of frame from here }else if (offense.size()==0){ System.out.println("You have won the battle\n"); //############ Force close of frame from here }` where does frame.setdefaultcloseoperation(jframe.dispose_on_close) and frame.dispose go? in the FightFrame or FightScene? – user2556304 Jul 24 '13 at 08:46
  • 1
    there isn't defined something about, then HIDE_ON_CLOSE is default close operation == setVisible(false), I'd be going about **nothing on close** or add WindowListener that can protect against... – mKorbel Jul 24 '13 at 08:48
  • I understand but how do I trigger this withing FightScene() (which is a JPanel)? – user2556304 Jul 24 '13 at 08:49
  • I have a new problem now.. sorry.. inside FightScene() in the checkCollisionMethod()... even though the parent JFrame closes.. I still have the program looping through "you won" or "you lost" how can I completely shut off the jpanel instead such that it goes out of scope and gets GC'd? – user2556304 Jul 24 '13 at 09:04
  • Ie. I just want the "You won" to print once, and thats it – user2556304 Jul 24 '13 at 09:05
  • 1
    @user2556304 Well are you using a timer or thread? if so this must be stopped or else it will carry on, alternatively make Thread/timer an observer i.e it looks to see if `JFrame` is visible if it is carry on running if not exit. Of course now you must make sure the `JFrame` is visible when timer is started... – David Kroukamp Jul 24 '13 at 09:06
  • @user2556304 See [this](http://stackoverflow.com/questions/15285875/killing-all-processes-force-everything-to-stop/15291905#15291905) similar question and answer – David Kroukamp Jul 24 '13 at 09:07
  • 2
    Cleaner than passing an instance of the frame: ``getTopLevelAncestor().setVisible(false)`` – Tim Boudreau Jul 24 '13 at 09:07
  • @TimBoudreau +1 for another way see my update shows `JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);` not sure about your `setVisible(false)` though – David Kroukamp Jul 24 '13 at 09:08
  • 1
    ty your psychic bro, I forgot allll about the timer – user2556304 Jul 24 '13 at 09:08
  • @user2556304 I created many of my own swing games so i ran into the exact problem you have and forgot about them many times too :P – David Kroukamp Jul 24 '13 at 09:09
0

((Frame)FightScene.this.getTopLevelAncestor()).dispose(); // FightScene is the JPanel