0

I'm stuck. I have a JFrame that, upon clicking on a JButton, launches a JApplet that is in another JFrame . What I am wondering is how I can close the JFrame in which the JApplet is in upon clicking on an "Exit" JButton in the JApplet.

Edit:I sliced down my code to show what I mean.

 import javax.swing.JFrame;

 public class Example {


 public static void main(String[] args) {
    Menu frame = new Menu();
    frame.setTitle("Menu");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setResizable(false);
    frame.setVisible(true);

    }
 }

.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Menu extends JFrame{

private JButton jbt=new JButton("Applet");

public Menu(){
    add(jbt);

    ButtonListener listener=new ButtonListener();
    jbt.addActionListener(listener);

}
public static void run(JApplet applet, int width, int height,String title){
    JFrame myFrame = new JFrame();
    myFrame.setTitle(title);
    myFrame.getContentPane().add(applet);
    myFrame.setSize(width, height);
    myFrame.setResizable(true);
    myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    applet.init();
    applet.start();
    myFrame.setVisible(true);
}

class ButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource()==jbt)
        {
            run(new myApplet(),400,180,"Applet");
        }

    }
 }
}

.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class myApplet extends JApplet{

        private JButton jbtExit=new JButton();

    @Override
    public void init(){
        add(jbtExit);
        jbtExit.addActionListener(new ButtonListener());
    }

    class ButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource()==jbtExit)
        {
            setVisible(false);
        }

    }
 }
7
  • 1
    Applets are not embedded in a JFrame, they are displayed on a web page. So I don't understand your design or question.
    – camickr
    Oct 31, 2013 at 5:14
  • 1
    @camickr Although they aren't meant to be embedded within JFrames, applets can be. After all, there's not much difference (as far as the applet is concerned) whether it's loaded by a web page, or by a frame imitating the functionality of one, but with the applet run on the frame's JVM.
    – FThompson
    Oct 31, 2013 at 5:16
  • Sorry what I meant is that the JFrame has a JApplet as content.
    – Progaine
    Oct 31, 2013 at 5:17
  • I guess my comment is why would you design something like this? What are you trying to do? Generally you add panels to a frame. Yes a JApplet is just a component by why are you using it instead of a panel?
    – camickr
    Oct 31, 2013 at 5:24
  • I have an assignment where I have to write a java application that displays to the screen a menu. From this menu I must have buttons that call certain applets to display user interfaces.
    – Progaine
    Oct 31, 2013 at 5:28

1 Answer 1

1

See Closing an Application for some general ideas.

You should be able to use the ExitAction for your Exit button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.