• Post Reply Bookmark Topic Watch Topic
  • New Topic

When I try to close a GUI window, all windows are closed

 
Jan Seer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am currently working on a small game with a GUI. On the program startup, I create a window which serves as main menu. When I press a button in this window, a second window (to change options) is opened.
When I press the button in the options window, the options window should be closed. But the main menu window is closed, too. Can somebody tell me what I am doing wrong?

Here is my code (I removed everything which seemed not necessary to me):

First: the "main" method which opens the first window



Second: the MainMenuWindow class


Third: the OptionsWindow class:

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you've designed your own window (that "Display" class) and even your own event-handling loop (those spinning while-loops). I don't see any Swing code at all there. Anyway chances are that it's something to do with the code of your Display class.

Although I have to say I find it strange that both of your windows use



to create their Display objects; that code looks like it uses the same Display object for both of them. If that were the case then maybe it wouldn't be surprising that closing one of them closed the other one, but again that would require knowledge of the Display class to be certain.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It looks like you've designed your own window (that "Display" class) and even your own event-handling loop (those spinning while-loops). I don't see any Swing code at all there. Anyway chances are that it's something to do with the code of your Display class.

I saw "Shell" in there which makes me think Jan is using SWT, not Swing.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, I'd forgotten that SWT was on-topic here. And obviously I don't know anything about SWT, so it's possible that the rest of my post is off target as well.

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However I'm going to let my guess that you should only dispose a Display once stand; I don't have a suggestion about what Jan should do instead though.
 
Jan Seer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, thank you for your answers, I was offline for some days.

Yes, I use SWT. The reason is that when I tried to get information about Java GUIs, I found that there are AWT, SWT and Swing. I did not really know what would be the best, so I simply started with SWT. There is also a quite convenient WYSIWYG editor for SWT in Eclipse, which created most of the basic GUI code for me (e.g. positioning and labeling of objects). It also created the "open()" methods.

I tried Pauls idea and changed the name of one of the "display" variables, but that did not resolve the problem (it neither created new ones as well).
As far as I understand, there should not be a problem by naming the "Display" variables the same. They have no "public" in front of them, so they should be class-private, am I right? And "getDefault()" is a static method of the "Display"-class.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one Display object in your program (as far as I know). It makes no difference how many variables you create referring to that object, there's only the one object. And no, the names of two variables in different scopes can be the same or different, it makes no difference because they are different variables which are unrelated. Your problem appears to be that you're disposing the Display object before you're finished using it.

As for SWT versus Swing, I don't think I've seen a question here about SWT for years. Unfortunately I don't know anything about SWT beyond what you just said, so any help I can give is just guesswork. You might want to switch to Swing, since at least there are people who can answer questions about it. You're still going to run into problems because of your weakness in basic Java, but that's pretty normal as far as I can see.
 
Jan Seer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, now I got my error!

Of course, there is only one instance of "Display"! I have to dispose the current shell, not the whole display. So instead of "display.dispose()" I have to use "shlAufgabenerstellung.dispose()". Now it works!

Maybe it would be better to use swing instead. Can you recommend some Swing tutorials? I did not find good ones yet.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually the Oracle tutorials are the best ones to work from (although there are a few exceptions). The Swing tutorial starts here: http://docs.oracle.com/javase/tutorial/uiswing/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!