0

I've been testing my JApplets and they have been working fine. I discovered the Applet menu that always loads and I wanted to test out its menu items. When I tried the Restart menu item, I realized components such as JButton, JLabel, JTextBox were duplicating themselves and text that was set by an ActionEvent did not reset. Here is a sample JApplet:

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class ExampleJApplet extends JApplet implements ActionListener
{
    private JLabel label= new JLabel();
    public void init()
    {
        setLayout(new FlowLayout());
        JButton button = new JButton("Press");
        add(button);
        add(label);
        button.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        label.setText("The button was pressed");
    }
}

It works, but when I go to Applet - Restart, the JButton duplicates itself and the JLabel does not reset. I thought that init() was called only once, but with Restart, it is called more than once. So that my JApplets don't do this on Restart, should I declare and add components (not referred by other methods such as actionPerformed()) in a constructor or should I declare all components at class scope so they aren't treated as new Objects? Should instantiations be done in init() so that updated text is replaced by initialized text?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Out of curiosity: why are learning about japplet? Are you doing classes in the history of computer science? – GhostCat Mar 21 '19 at 04:03
  • 1
    *"I discovered the Applet menu that always loads.."* So .. you're talking about the applet viewer, right? Why care so much about how a developer tool is working (or malfunctioning)? It seems Oracle doesn't care that much about applets (given they've been deprecated) and I'd guess even less about artifacts related to the developer tools for applets. To expand on what @GhostCat mentioned.. See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). – Andrew Thompson Mar 21 '19 at 13:40
  • I took Java years ago. We did learn about JApplets. Maybe Java textbooks don't even have a chapter on JApplets anymore or if they do, the instructors just skips it. I don't they are depreciated from Java. The Oracle website still has the JApplet class on there. Browsers have stopped supporting JApplet, but JApplet is still in the JDK and they are still runnable. Companies probably stopped working with JApplets and that might be why it's useless to teach it. So it really doesn't matter how I crash proof them against Restart since they are as a hobby now and not what companies will want. – Mike Mendizabal Mar 22 '19 at 04:10

0 Answers0