Article View: borland.public.jbuilder.java.language
Article #18621Re: two frames instead of one
From: Michael J. Rozlog
Date: Mon, 11 May 1998 00:00
Date: Mon, 11 May 1998 00:00
308 lines
9344 bytes
9344 bytes
Here the code I posted a long time ago, it will explain the JBCL
problem. I hope it helps,
Mike
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
Oyvind L Johansen wrote:
>
> I have a strange problem.
>
> I have a buttonbar which when you click on one of the buttons it fires an
> event calling this code:
>
> else if(actionCommand.equals("New")) {
> System.out.println("new called");
> NewFrame f = new NewFrame (this, "New Frame", true);
> f.show();
> }
>
> The strange thing is when i close this frame it pops up once again. When I
> close this frame nothing happens (as supposed). The same happens if I call
> this code from a menuitem. "new called" gets printed twice.
>
> Anybody having any idea?
>
> Thanks
> Oyvind
>
> --
> --------------------------------------------------------------
> Oyvind L Johansen
> Tabermatics Inc.
Technical Details
Message-ID:
<35568F4D.ECA06EA6@email.msn.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6ivbtb$39r1@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,b67eb006d7bb7e7d X-Google-Attributes: gidf74b3,public From: "Michael J. Rozlog" <rozm@email.msn.com> Subject: Re: two frames instead of one Date: 1998/05/11 Message-ID: <35568F4D.ECA06EA6@email.msn.com> X-Deja-AN: 352120305 Content-Transfer-Encoding: 7bit References: <6ivbtb$39r1@forums.borland.com> Mime-Version: 1.0 Reply-To: rozm@msn.com Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language