Closing a Dialog Box within a Frame

When a user clicks a button I want them to see a Dialog Box pop up giving them a message saying "Debeg Mode is Enabled". I have a panel that has a label and a button that says ok. When the user hits ok I want the Dialog Box to go away. Unfortunately when I do that the box will go away but I get a stack Trace that says the following...... My code is attached below. I would like to know the correct way of closing a dialog box when a button within the dialog box so I won't get a stack trace. The most important code you will find is in the frameApplet class in the actionPerformed where the dialog box gets shown.

Button in buttonApplet class was pressed.

Dialog Button was pressed

An Illegal Exception Was caught.

java.security.AccessControlException: access denied (java.lang.RuntimePermission

exitVM)

at java.security.AccessControlContext.checkPermission(AccessControlConte

xt.java:272)

at java.security.AccessController.checkPermission(AccessController.java:

399)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)

at java.lang.SecurityManager.checkExit(SecurityManager.java:765)

at java.lang.Runtime.exit(Runtime.java:91)

at java.lang.System.exit(System.java:701)

at frameApplet.actionPerformed(frameApplet.java:55)

at java.awt.Button.processActionEvent(Button.java:329)

at java.awt.Button.processEvent(Button.java:302)

at java.awt.Component.dispatchEventImpl(Component.java:2595)

at java.awt.Component.dispatchEvent(Component.java:2499)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:134)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:101)

at java.awt.Dialog.show(Dialog.java:386)

at frameApplet.buildGui(frameApplet.java:38)

at frameApplet.<init>(frameApplet.java:17)

at buttonApplet.actionPerformed(buttonApplet.java:23)

at java.awt.Button.processActionEvent(Button.java:329)

at java.awt.Button.processEvent(Button.java:302)

at java.awt.Component.dispatchEventImpl(Component.java:2595)

at java.awt.Component.dispatchEvent(Component.java:2499)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:134)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:96)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:88)

Exception occurred during event dispatching:

java.lang.IllegalArgumentException: adding a window to a container

at java.awt.Container.addImpl(Container.java:339)

at java.awt.Container.add(Container.java:231)

at frameApplet.buildGui(frameApplet.java:40)

at frameApplet.<init>(frameApplet.java:17)

at buttonApplet.actionPerformed(buttonApplet.java:23)

at java.awt.Button.processActionEvent(Button.java:329)

at java.awt.Button.processEvent(Button.java:302)

at java.awt.Component.dispatchEventImpl(Component.java:2595)

at java.awt.Component.dispatchEvent(Component.java:2499)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:134)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:96)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:88)

Code

buttonApplet

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class buttonApplet extends Applet implements ActionListener {

Button _buttonSubmit = new Button("Submit");

frameApplet frames;

public void init() {

add(_buttonSubmit);

_buttonSubmit.addActionListener(this);

}

public void actionPerformed(ActionEvent evt) {

Object source = evt.getSource();

if (source == _buttonSubmit) {

System.out.println("Button in buttonApplet class was pressed.");

frames = new frameApplet();

}

}

}

frameApplet

import java.awt.*;

import java.awt.event.*;

public class frameApplet extends Frame implements ActionListener {

private WindowEvent windowEvent;

private Button _dialogButton;

private Label _labelDebug;

private Dialog _dialogWindow;

private Window _windowFrame;

private Panel _panelObjects;

public frameApplet () {

super();

buildGui();

}

private void buildGui() {

this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);

this.setBackground(Color.lightGray);

this.setForeground(Color.blue);

this.setLayout(new FlowLayout());

_labelDebug = new Label("Debug is now Enabled");

_dialogButton = new Button("Ok");

_dialogButton.addActionListener(this);

_panelObjects = new Panel();

_panelObjects.add(_labelDebug);

_panelObjects.add(_dialogButton);

_dialogWindow = new Dialog(this, "Information", true);

_dialogWindow.add(_panelObjects);

_dialogWindow.pack();

_dialogWindow.show();

this.setSize(150,75);

this.add(_dialogWindow);

this.setVisible(false);

}

public void actionPerformed(ActionEvent evt) {

try {

Object source = evt.getSource();

if (source == _dialogButton) {

System.out.println("Dialog Button was pressed");

_dialogWindow.hide();

this.hide();

_dialogWindow.dispose();

this.dispose();

System.exit(0);

}

}

catch(Exception e) {

System.out.println("An Illegal Exception Was caught.");

e.printStackTrace();

}

}

}

[6109 byte] By [garbersb] at [2007-9-26 2:41:37]
# 1

You call System.exit(0) when the user clicks OK, is that what you want, shutting down the virtual machine? Didn't you want just close that dialog?

In any case, it seems you are working in an applet, and browsers don't let applets to shut down their virtual machine, so the call to System.exit(0) causes the security exception.

It would be different in a stand-alone application.

fabiocar at 2007-6-29 10:17:31 > top of Java-index,Desktop,Core GUI APIs...
# 2

When I get rid of System.exit(0); I still get a runtime error. Now I get the following runtime error when closing the dialog box within the button. In my actionEvent with my button I end up just having the following...

public void actionPerformed(ActionEvent evt) {

try {

Object source = evt.getSource();

if (source == _dialogButton) {

System.out.println("Dialog Button was pressed");

//_dialogWindow.hide();

//this.hide();

_dialogWindow.dispose();

//this.dispose();

}

}

All I have is _dialogWindow.dispose();. It closes the window but now I get the following run time error.

I don't understand why this is? Can anyone help?

--

Button in buttonApplet class was pressed.

Dialog Button was pressed

Exception occurred during event dispatching:

java.lang.IllegalArgumentException: adding a window to a container

at java.awt.Container.addImpl(Container.java:339)

at java.awt.Container.add(Container.java:231)

at frameApplet.buildGui(frameApplet.java:40)

at frameApplet.<init>(frameApplet.java:17)

at buttonApplet.actionPerformed(buttonApplet.java:23)

at java.awt.Button.processActionEvent(Button.java:329)

at java.awt.Button.processEvent(Button.java:302)

at java.awt.Component.dispatchEventImpl(Component.java:2595)

at java.awt.Component.dispatchEvent(Component.java:2499)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:336)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:134)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:96)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:88)

garbersb at 2007-6-29 10:17:31 > top of Java-index,Desktop,Core GUI APIs...
# 3

I got rid of the run time problem. Instead of using dispose() I ended up using show() and hide(). When the initial GUI came up I had the hide() method called and when the button was pushed to bring up the dialog box show() was called. I then called hide() again when the ok button was pushed. This called no run -time error.

garbersb at 2007-6-29 10:17:31 > top of Java-index,Desktop,Core GUI APIs...