0

I've spent the last hour searching on Google to try and achieve something - preventing the closure of my main interface after one of the others is closed. I'm not sure if this is the correct way to word my question, so apologies for the confusion if there is any.

I have one interface called MainUI, which is the main interface for starting my Java program. On this interface is two buttons that opens two other interfaces (SinglePlayerStatsUI and MultiplePlayerStatsUI). If I click on either button, it opens the appropriate interface (as it should), but when I close the opened interface it also closes the main interface (MainUI) - which isn't what I want. I am wondering what would be a good solution to stop the main interface from closing (which is also the main class of the program)?

To save some space in this question, I have my Java project on GitHub (created in NetBeans) at https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats. The class file for the MainUI is called "MainUI.java", the class for SinglePlayerStatsUI is called "SinglePlayerStatsUI.java", and the class for MultiplePlayerStatsUI is called "MultiplePlayerStatsUI.java". These classes are located in https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats/src/pcricketstats.

I have screenshots of each interface also - these can be seen at https://i.stack.imgur.com/LwZ9l.jpg.

If my question needs more clarification, please let me know.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rob
  • 1,272
  • 6
  • 24
  • 35
  • 1
    You get much better help sooner if you provide a [SSCCE](http://sscce.org/). – Howard May 19 '13 at 09:25
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) And don't include sigs. in questions, they are noise. – Andrew Thompson May 19 '13 at 09:33
  • 1
    @AndrewThompson That's some good advice, don't know why I wasn't taught this earlier on... Will need to change my project slightly to make it more user friendly :). – Rob May 19 '13 at 09:54
  • I'm thinking of using the JTabbedPane idea - I would put information from my two interfaces into two tabs I'm taking it? – Rob May 19 '13 at 10:13
  • `JTabbedPane..` Yes, that could work. – Andrew Thompson May 19 '13 at 12:57
  • @AndrewThompson I've tested the JTabbedPane and it's much more user friendly - thanks for the advice :) – Rob May 19 '13 at 13:09

2 Answers2

4

Change line 100 of SinglePlayerStatsUI.java and line 88 of MultiplePlayerStatsUI.java

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

To

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

Or change line 5 of SinglePlayerStatsUI.form and MultiplePlayerStatsUI.form

<Property name="defaultCloseOperation" type="int" value="3"/>

To

<Property name="defaultCloseOperation" type="int" value="2"/>
johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • The above example made sense, but I can't get it to work in my program... I'm thinking about the advice @AndrewThompson gave in one of his comments about multiple JFrames - so this example might be redundant for me if I go down AndrewThompson's route (but still very good advice though). – Rob May 19 '13 at 10:11
2

This isn't really an answer, but since I have found a better solution to what I wanted, I thought I would close this question off...

I had a look into JTabbedPanes and it looks so much friendlier than opening up multiple JFrames. AndrewThompson gave some very useful advice, and referred me to the link "The Use of Multiple JFrames, Good/Bad Practice?" - which gave me the reason for switching to JTabbedPanes.

Thanks for the feedback received, and I hope someone else also learns from this :).

Community
  • 1
  • 1
Rob
  • 1,272
  • 6
  • 24
  • 35