Results 1 to 4 of 4
  1. #1
    Arsenic is offline Member
    Join Date
    Mar 2009
    Location
    Atlantis
    Posts
    8
    Rep Power
    0

    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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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.

  3. #3
    Arsenic is offline Member
    Join Date
    Mar 2009
    Location
    Atlantis
    Posts
    8
    Rep Power
    0

    Default

    Quote 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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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).

    Java 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;
        }
      }
      
    }

Similar Threads

  1. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 04:00 PM
  2. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:17 AM
  3. Closing packages
    By uncopywritable in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 11:47 PM
  4. JFrame vs Applet
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •