• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Dispose Window & jar

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I have two problems. First: I have an application, which has a main window, let's call it "Main" (a frame). From Main, i can open other windows. The problem is that when i close Main, the other windows remain open, don't close along with it. What i want to do is the following: each window to close individually(which already happens), except for Main, which is supposed to close all other opened windows along with it. How do i do that?

The second problem regards putting images in a jar file. Each window in my project contains the same four images. When i want to make a jar of my application, these images are not included. How can i put the images into the jar files? I work with netbeans, if anyone knows how to do that there, since i don't know how to work with command lines (i don't know where to write them)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you close the frame Main do you just want to close the other windows or do you want to stop the application?

The second question has been asked many times. If you search for it you'll probably find what you need.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to stop the application
 
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look for the closing behaviour of JFrames. Look in the Java tutorials and scroll down to "top-level" and Frames.
As far as I remember, you can exit, dispose, or hide a frame when you close it. Or you can do nothing. The default is hide.
Now the question is, are there likely to be any other threads running when you close it. If there are, you need to allow them to finish before closing the JVM. In which case exit would be dangerous. Imagine your other thread is writing your results to a file/database and you shut the JVM down before it has finished
Do the other windows have an independent existence? If they are top-level, they probably do. So the same problems about closing them applies.

Anyway, have a look at the tutorials, and the JFrame class, see what they tell you, and come back when you don't understand it.

I am moving this discussion to our GUIs forum.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not have any threads in my program. Each window has it's own behaviour. Main consists of a menu, in which each menu item opens a new window.
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What format are these other windows? Are they top-level containers in their own right? Do they close if you close the main frame?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what i want to do and can't: close them when the main frame closes
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried a WindowListener? You can close them from its closing or closed methods. You may need a recursive close method, whereby all windows opened secondarily are closed from a window's close method.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally an application has a single JFrame as the main window and then you use JDialogs (specifying the frame as the owner) for child windows. In this case if you close the frame the dialogs will also close.

 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have more jFrames and one from which all open
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And presumably you keep a reference to those frames from the original class whence you opened them. Then you can call methods on them to get rid of them.
Start by trying its dispose() method. I don't know whether you have to override that to dispose the secondary frames or not. You will have to try it.

If that works, you can set the Frame to DISPOSE_ON_CLOSE and you will probably get all the frames closing in an orderly fashion, without terminating any threads prematurely.Dispose is much less vicious than exit, so there’s less risk of leaving a task half-finished.
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does say dispose() gets rid of all children, but I am not sure whether that includes secondary frames.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Window.getWindows() to get all windows opened by in the current JVM.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually have no idea on what to do. I tried to add a window adapter and detect the closing operation of the main window. There, i tried to close all windows that were opened from it. But it does not work. And i don't know how to work with dialogs. I didn't quite understand that, and i'm not sure that i can actually work with dialogs, since some windows must be accessed from other ones, different than Main
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working with a dialog is the same as working with a frame.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could i do maybe something to put everything that i have in a dialog, without having to modify anything to its content?(i'm talking about the content of a frame)
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The windows did not close. But maybe i used the wrong closing operation, i will study more the JFrames
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you have window 1 and something opens window 2? Does anything associated with window 1 retain a reference to window 2? I would be suspicious of your creating lots of secondary windows without some sort of link between them. Even so, you might find yourself having to add a WindowListener and dispose of all those windows in the closing or closed method. You would need WindowListeners on those secondary windows too . . .

The suggestion about using Dialogs sounds good. But one little thing about Dialogs; some of them are modal. You can find out more in the Java Tutorials.
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, Rob's suggestion about getWindows() will allow you access to those secondary windows, and you can dispose of them all.
 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will look into that and see if i can use it. Thank you for the suggestion
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The dialog is still the simplest solution. No special logic is needed.

I don't know why your windows didn't close because you haven't posted a SSCCE that demonstrates the problem. It only takes a couple of lines of code to test this.

 
Rus Corina
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob Camick : I don't really know how to apply that to my program. I have about 6 JFrames (without the main one), very complicate with lots of variables, and can't combine the code for them into just one Main class.



I posted it the main class, called "AplicatieClient" (I considered names to be not very important, they're in my language).
 
Rob Spoor
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not an SSCCE. For instance, I don't have classes ResultSetTableModel or TipAparat.
 
Campbell Ritchie
Marshal
Posts: 79660
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:That's not an SSCCE. . . .

But I can see some serious design problems in it:
  • Don't call the setDefaultCloseOperation method in a Listener. You should set that when the GUI is created, eg in the display's constructor or getUpGUI method.
  • You have empty catch blocks. Those are dangerous because you never know whether an Exception has occurred.
  • You have anonymous Listeners doing the same thing. You ought to create a DatabaseActionListener (or similar) class
  • You also have long //comments inside code blocks, which I have changed because they make the code difficult to read. Your indentation is inconsistent, with "try" and the line following at the same level.
     
    Rus Corina
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Rob Spoor wrote:That's not an SSCCE. . . .

    But I can see some serious design problems in it:
  • Don't call the setDefaultCloseOperation method in a Listener. You should set that when the GUI is created, eg in the display's constructor or getUpGUI method.
  • You have empty catch blocks. Those are dangerous because you never know whether an Exception has occurred.
  • You have anonymous Listeners doing the same thing. You ought to create a DatabaseActionListener (or similar) class
  • You also have long //comments inside code blocks, which I have changed because they make the code difficult to read. Your indentation is inconsistent, with "try" and the line following at the same level.



    The long comments i just added now, just to explain what's wrong. The catch blocks were empty by mistake . In other classes in catch blocks i put printStackTrace(). I also forgot to format the source code, thus inconsistent indentation. About the other mistakes, i don't know what to do about them. I don't know where the setDefaultCloseOperation should go, and I don't really know what an DatabaseActionListener does or how to use it, but I will look it up. I know it is not a SSCE but I thought that maybe it could be enough
     
    Rus Corina
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:So you have window 1 and something opens window 2? Does anything associated with window 1 retain a reference to window 2? I would be suspicious of your creating lots of secondary windows without some sort of link between them. Even so, you might find yourself having to add a WindowListener and dispose of all those windows in the closing or closed method. You would need WindowListeners on those secondary windows too . . .

    The suggestion about using Dialogs sounds good. But one little thing about Dialogs; some of them are modal. You can find out more in the Java Tutorials.



    I am quite new at this. I'm still in college, and someone found me a small project for a small company. It is mainly just for keeping evidence of different information (add, delete, modify and search). I don't really know anything about all the conventions and techniques used in making Java projects, since in school we don't learn much about it. If you could please explain your idea above (that I quoted here), it would be very helpful. How can I establish some link between them? How do I dispose all those windows in the closing or closed method? Could you please give an example?
     
    Campbell Ritchie
    Marshal
    Posts: 79660
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can't look up DatabaseActionListener. It doesn't exist, at least not until you write it.

    JFrame myFrame = new JFrame("Corinna");
    myFrame.setBounds(50, 50, 250, 150);
    myFrame.setDefaultCloseOperation(JFrame.EXPLODE_ON_EXIT); //
    // Other code to add all the other components
    myFrame.setVisible(true);


    You are probably still better off using Dialogs.

    That is one way to do it. You can find the dispose method here.

    No, it is not suitable for an SSCCE. It must be possible for others to see the SSCCE working. We cannot compile or try it if you don't supply every class.
    The problem is not that you had comments, but that the comments are too long. I shall send you a PM to explain about long comments.
     
    Rus Corina
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell Ritchie : I couldn't have supplied every class, since they are really long. I will try your suggestion and see if it works. Seems pretty good
     
    Rus Corina
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie : it worked. Thank you very much
     
    Campbell Ritchie
    Marshal
    Posts: 79660
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done ... and "You're welcome". And sorry for writing Corinna instead of Corina.
     
    Rus Corina
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No problem about the name. Actually, in english my name is Corinne, so you can say you got the "a" wrong
     
    Campbell Ritchie
    Marshal
    Posts: 79660
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you. I have met people called Corinne and Corinna.
     
    World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic