Article View: borland.public.jbuilder.java.language
Article #19972Re: Closing frame windows...
From: Michael J. Rozlog
Date: Sun, 09 Nov 1997 00:00
Date: Sun, 09 Nov 1997 00:00
312 lines
9936 bytes
9936 bytes
John,
This is rather a lengthy response to your question... and the answer does not
come till closer to the end. This was a response to an earlier question about
objects and garbage collection however, it does give you the solution to your
problem with multiple frames... if you have any other questions just ask!
ANSWER:
If you want the frame to be centered you want to copy the code before the
first frame's setVisible() method. When you create a frame it does not
have the systems screen information you must get it and you must set this
up manually. Example:
This is the Calling program with the main in it.
//Title: Your Product Name
//Version:
//Copyright: Copyright (c) 1997
//Author: Michael Rozlog
//Company: rozm@msn.com
//Description:Your description
package checkit;
import java.awt.*;
public class checkLab {
boolean packFrame = false;
//Construct the application
public checkLab() {
frmMainCheck frame = new frmMainCheck();
//Pack frames that have useful preferred size info, e.g. from their
layout
//Validate frames that have preset sizes
if (packFrame)
frame.pack();
else
frame.validate();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
static public void main(String[] args) {
new checkLab();
}
}
This is the called program that calls the second frame.
//Title: Your Product Name
//Version:
//Copyright: Copyright (c) 1997
//Author: Michael Rozlog
//Company: rozm@msn.com
//Description:Your description
package checkit;
import java.awt.*;
import java.awt.event.*;
import borland.jbcl.control.*;
import borland.jbcl.layout.*;
//import secondFrame;
public class frmMainCheck extends DecoratedFrame {
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout2 = new XYLayout();
BevelPanel bevelPanel1 = new BevelPanel();
StatusBar statusBar = new StatusBar();
ButtonControl buttonControl1 = new ButtonControl();
//Construct the frame
public frmMainCheck() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
public void jbInit() throws Exception{
this.setLayout(borderLayout1);
this.setSize(new Dimension(467, 360));
this.setTitle("Check Example");
buttonControl1.setLabel("Open");
buttonControl1.addActionListener(new
frmMainCheck_buttonControl1_actionAdapter(this));
bevelPanel1.setLayout(xYLayout2);
this.add(statusBar, BorderLayout.SOUTH);
this.add(bevelPanel1, BorderLayout.CENTER);
bevelPanel1.add(buttonControl1, new XYConstraints(185, 115, -1, -1));
}
void buttonControl1_actionPerformed(ActionEvent e) {
boolean packFrame = false;
secondFrame f = new secondFrame();
f.pack();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = f.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
f.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height -
frameSize.height) / 2);
f.setTitle("Second Frame");
f.setVisible(true);
}
}
class frmMainCheck_buttonControl1_actionAdapter implements
java.awt.event.ActionListener {
frmMainCheck adaptee;
frmMainCheck_buttonControl1_actionAdapter(frmMainCheck adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.buttonControl1_actionPerformed(e);
}
}
Now the Second Frame that is called from the button1_actionPerformed event
//Title: Your Product Name
//Version:
//Copyright: Copyright (c) 1997
//Author: Michael J. Rozlog
//Company: rozm@msn.com
//Description: Your description
package checkit;
import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
public class secondFrame extends DecoratedFrame {
XYLayout xYLayout1 = new XYLayout();
GridControl gridControl1 = new GridControl();
ButtonControl buttonControl1 = new ButtonControl();
public secondFrame() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
void jbInit() throws Exception{
this.setExitOnClose(false);
xYLayout1.setWidth(500);
xYLayout1.setHeight(300);
buttonControl1.setLabel("Close");
buttonControl1.addActionListener(new
secondFrame_buttonControl1_actionAdapter(this));
this.setLayout(xYLayout1);
this.add(gridControl1, new XYConstraints(43, 19, 364, 154));
this.add(buttonControl1, new XYConstraints(180, 209, -1, -1));
}
void buttonControl1_actionPerformed(ActionEvent e) {
this.dispose();
}
protected void processWindowEvent(WindowEvent parm1) {
//TODO: override this borland.jbcl.control.DecoratedFrame method;
if (parm1.getID() == WindowEvent.WINDOW_CLOSING && getParent() == null
&& !exitOnClose) {
this.dispose();
}else {
super.processWindowEvent( parm1);
}
}
}
class secondFrame_buttonControl1_actionAdapter implements
java.awt.event.ActionListener {
secondFrame adaptee;
secondFrame_buttonControl1_actionAdapter(secondFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.buttonControl1_actionPerformed(e);
}
}
Notice the extra code on the second frame:
protected void processWindowEvent(WindowEvent parm1) {
//TODO: override this borland.jbcl.control.DecoratedFrame method;
if (parm1.getID() == WindowEvent.WINDOW_CLOSING && getParent() == null
&& !exitOnClose) {
this.dispose();
}else {
super.processWindowEvent( parm1);
}
}
The reason for this code, have you notice if you hit the "x" method at the
top right hand corner it closes the whole application, even if you have
exitOnClose set to false on the second frame. This is most likely a bug!
To get around this, you can use a wizard to help you with the writing of
the code. In the second frame, while in the source, go to the wizard menu
item and choose the override wizard. From there, click the "+" symbol on
the DecoratedFrame and select the processWindowEvent and select "ok". This
will put the appropriate code into the program. Now lets look at the logic
in the Decorated frame by going to the structure pane and double-clicking
on the DecoratedFrame object, this of-course takes us to the source for the
DecoratedFrame now select the blue arrow icon which represents a method for
the processWindowEvent. The cursor should be located on the event, it
should look like this:
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING && getParent() == null &&
exitOnClose)
System.exit(0);
}
If you test it out you will come to the conclusion that it should be
(not)exitOnClose or !exitOnClose. The code is telling us to close the
frame regardless of the exitOnClose value. To un-display a frame you call
its dispose() method. By the by, when you call dispose() it does not do
garbage collection on the frame unless it is out of scope. We modify the
if statement to check if exitOnClose is not true then just displose(), else
we call the super method and continue regular processing.
I hope this helps, if you have any other specific question don't hesitate
to email me.
Mike
rozm@msn.com
John Papadomichelakis wrote:
> Hi...
>
> In my application, I need a main window and child windows.
> What I want is a behavior similar to an MDI application.
> Is it possible?
>
> I can open frame windows from a frame window.
> The problem is that when one of them is closing, it closes the
> application
> O set the property not to call system.exit() on window close but then
> the window
> never closes!
>
> So...
> I guess I should catch the event that occurs when the window closes...
>
> What's the best approach for this?
>
> --
>
> Papadomichelakis John
> "Unite for Java! - http://www.javalobby.org"
> __________________________________________________
> email : michelos@freemail.gr
> URL : http://users.spark.net.gr:8080/~michelos/
> TEL : 30 31 758 429, 30 31 709 796
> __________________________________________________
>
> ------------------------------------------------------------------------
>
> John Papadomichelakis <michelos@freemail.gr>
> BIA
>
> John Papadomichelakis
> BIA <michelos@freemail.gr>
> HTML Mail
> 9 Filippou Netscape Conference Address
> Euosmos Netscape Conference DLS Server
> Thessaloniki
> 56224
> Greece
> Additional Information:
> Last Name Papadomichelakis
> First NameJohn
> Version 2.1
Technical Details
Message-ID:
<34654F8E.B4C5EDB1@msn.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<346493C4.8E07FFC6@freemail.gr>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,94bc9b1ceb724919 X-Google-Attributes: gidf74b3,public From: "Michael J. Rozlog" <rozm@msn.com> Subject: Re: Closing frame windows... Date: 1997/11/09 Message-ID: <34654F8E.B4C5EDB1@msn.com> X-Deja-AN: 288019964 References: <346493C4.8E07FFC6@freemail.gr> Organization: None Newsgroups: borland.public.jbuilder.java.language