aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Dispose Window & jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dispose Window & jar" Watch "Dispose Window & jar" New topic
Author

Dispose Window & jar

Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
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)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
i want to stop the application
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Jul 08, 2011
Posts: 90
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
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Jul 08, 2011
Posts: 90
That's what i want to do and can't: close them when the main frame closes
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
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

Joined: Jul 08, 2011
Posts: 90
i have more jFrames and one from which all open
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
It does say dispose() gets rid of all children, but I am not sure whether that includes secondary frames.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You can use Window.getWindows() to get all windows opened by in the current JVM.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
Working with a dialog is the same as working with a frame.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
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)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4547
    
    5

What happened when you tried it?


luck, db
There are no new questions, but there may be new answers.
Rus Corina
Ranch Hand

Joined: Jul 08, 2011
Posts: 90
The windows did not close. But maybe i used the wrong closing operation, i will study more the JFrames
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Jul 08, 2011
Posts: 90
I will look into that and see if i can use it. Thank you for the suggestion
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
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

Joined: Jul 08, 2011
Posts: 90
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

That's not an SSCCE. For instance, I don't have classes ResultSetTableModel or TipAparat.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

    Joined: Jul 08, 2011
    Posts: 90
    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

    Joined: Jul 08, 2011
    Posts: 90
    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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38910
        
      23
    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

    Joined: Jul 08, 2011
    Posts: 90
    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

    Joined: Jul 08, 2011
    Posts: 90

    Campbell Ritchie : it worked. Thank you very much
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38910
        
      23
    Well done ... and "You're welcome". And sorry for writing Corinna instead of Corina.
    Rus Corina
    Ranch Hand

    Joined: Jul 08, 2011
    Posts: 90
    No problem about the name. Actually, in english my name is Corinne, so you can say you got the "a" wrong
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38910
        
      23
    Thank you. I have met people called Corinne and Corinna.
     
    wood burning stoves
     
    subject: Dispose Window & jar