Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2009, 06:19 PM
Member
 
Join Date: Mar 2009
Location: Atlantis
Posts: 8
Rep Power: 0
Arsenic is on a distinguished road
Default Closing Popup JFrame in Applet
So I've coded everything I need using the SE 6 in notepad++. My program allows the user to choose from a few font options, and then display the string in a JFrame pop up window along with an image. When I click the X on the pop up window, select a different set of characteristics for the string, and hit the Ok button it displays the past image and string, along with the new image and string.

I was just wondering how I could close the JFrame to purge the previous text and image displayed. I tried using .dispose();, as well as .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); and even though it allows me to compile, when I hit the X an exception is thrown (I'd post the exception, but I'm at school and don't have the code with me currently).

Any help is much appreciated!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-10-2009, 06:30 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,502
Rep Power: 8
Fubarable is on a distinguished road
Default
1) I would strongly urge you not to use a JFrame here but rather a JDialog. It can do most anything a JFrame can do, and behaves better with JApplets.
2) It's kind of hard to know what further advice to give you without being able to see the complete error message. If you are still having trouble when you get home, why not post this message?

Best of luck.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-10-2009, 09:00 PM
Member
 
Join Date: Mar 2009
Location: Atlantis
Posts: 8
Rep Power: 0
Arsenic is on a distinguished road
Default
Originally Posted by Fubarable View Post
1) I would strongly urge you not to use a JFrame here but rather a JDialog. It can do most anything a JFrame can do, and behaves better with JApplets.
2) It's kind of hard to know what further advice to give you without being able to see the complete error message. If you are still having trouble when you get home, why not post this message?

Best of luck.

Alas I'm home and I lied. It's when I hit the ok button that no frame pops up, and the exception is displayed (when the dispose(), and other function are included):


Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
at java.security.AccessControlContext.checkPermission (AccessControlContext.java:323)
at java.security.AccessController.checkPermission(Acc essController.java:546)
at java.lang.SecurityManager.checkPermission(Security Manager.java:532)
at java.lang.SecurityManager.checkExit(SecurityManage r.java:744)
at javax.swing.JFrame.setDefaultCloseOperation(JFrame .java:377)
at FlagApplet$7.actionPerformed(FlagApplet.java:66)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6216)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
at java.awt.Component.processEvent(Component.java:598 1)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4583)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 13)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4150)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Component.dispatchEvent(Component.java:44 13)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-10-2009, 09:37 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,502
Rep Power: 8
Fubarable is on a distinguished road
Default
Are you trying to access files that are local to the client computer in any way from the applet or its generated JFrame? If not, I'm not sure why you're getting this error, but it sounds as if your applet is trying to do things outside of its sandbox.

This is my SSCCE to demonstrate showing and disposing of JFrames and JDialogs from within a JApplet. If this doesn't help, you may wish to create and post your own SSCCE (click on link to learn how).

Code:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JAppletAndFrame extends JApplet
{
  private static final String SHOW_FRAME = "Show JFrame";
  private static final String SHOW_DIALOG = "Show JDialog";

  public void init()
  {
    try
    {
      javax.swing.SwingUtilities.invokeAndWait(new Runnable()
      {
        public void run()
        {
          createGUI();
        }
      });
    }
    catch (Exception e)
    {
      System.err.println("createGUI didn't successfully complete");
    }
  }

  private void createGUI()
  {
    JButton showFrame = new JButton(SHOW_FRAME);
    JButton showDialog = new JButton(SHOW_DIALOG);
    BtnListener btnListener = new BtnListener();
    showFrame.addActionListener(btnListener);
    showDialog.addActionListener(btnListener);
    
    JPanel panel = new JPanel();
    panel.add(showFrame);
    panel.add(showDialog);
    getContentPane().add(panel);
  }
  
  private class BtnListener implements ActionListener
  {
    private JFrame frame;
    private JDialog dialog;
    
    public void actionPerformed(ActionEvent e)
    {
      String command = e.getActionCommand();
      if (command.equals(SHOW_FRAME))
      {
        if (frame == null)
        {
          frame = new JFrame("Frame");
          frame.add(createPanel());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        }
        else
        {
          frame.setVisible(true);
        }
      }
      else if (command.equals(SHOW_DIALOG))
      {
        if (dialog == null)
        {
          Window window = SwingUtilities.getWindowAncestor(JAppletAndFrame.this);
          dialog = new JDialog(window, "Dialog", ModalityType.MODELESS);
          dialog.add(createPanel());
          dialog.pack();
          dialog.setLocationRelativeTo(null);
          dialog.setVisible(true);
        }
        else
        {
          dialog.setVisible(true);
        }
        
      }
    }

    private JComponent createPanel()
    {
      final JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(400, 300));
      JButton disposeBtn = new JButton("Dispose");
      disposeBtn.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          Window window = SwingUtilities.getWindowAncestor(panel);
          window.dispose();
        }
      });
      panel.add(disposeBtn);
      return panel;
    }
  }
  
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing an Editor janpol1 Eclipse 0 02-14-2009 05:00 PM
JFrame (closing under a condition) Java Tip Java Tips 0 03-12-2008 12:17 PM
Closing packages uncopywritable New To Java 0 08-14-2007 12:47 AM
JFrame vs Applet baltimore AWT / Swing 1 08-06-2007 04:24 AM


All times are GMT +2. The time now is 03:41 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org