0

I am currently trying to get a MouseListener to listen to a java applet, however it is not working I am unsure about what is wrong with the following code, would anyone be able to help find the error? Also, I realize that java applets are pretty much depreciated, however it is necessary for the certain thing I am doing.

public class loader extends JPanel {
    private createApplet applet= new createApplet();

    public loader(final boolean oldschool) {
        setLayout(new BorderLayout());

        add(applet.getApplet(), BorderLayout.CENTER);

        MouseListener test = new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("test");
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("test");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("test");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("test");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("test");
            }
        };

        // Adds test mouse listener to instance of applet
        applet.addMouseListener(test);
        revalidate();
    }

}

class createApplet extends Applet implements AppletStub {
    Applet applet = this;

    public createApplet () {

        downloadAndCreate();
    }

    public void downloadAndCreate() {

        // Code that downloads applet and sets it etc here


        /*
            Set the applet stub
         */
        applet.setStub(this);

        /*
            Initialize the applet
         */
        applet.init();

        /*
            Start the applet
         */
        applet.start();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
uncoded
  • 113
  • 1
  • 1
  • 10
  • It is not printing test to the command line or detecting it at all. Also, isn't it inside the loaders constructor? – uncoded Apr 13 '16 at 02:34
  • You're right it is, the indentation threw me off. – Jonny Henly Apr 13 '16 at 02:37
  • Ah, sorry about that. When I uploaded it originally I completely butchered it and messed up all indenting I think Johny Henly fixed it for the most part, thanks for that. – uncoded Apr 13 '16 at 02:39
  • No problem at all, I noticed that your adding the applet to the loader and not the other way around. I'm not to familiar with applets, but aren't you suppose to add the `JPanel` to the applet? The comments and answer on the SO question: [How to add a applet to a JPanel](http://stackoverflow.com/questions/13398009/how-to-add-a-applet-to-a-jpanel-in-java) say that Applet is a top level container. – Jonny Henly Apr 13 '16 at 02:40
  • Hmm, the only problem with that is, what I am trying to do is take an already created applet and embed it almost onto a JPanel where I will add various other components to that JPanel. So far I am able to do that quite easily, but getting a mouse listener to listen for mouse clicks only on the applet component seems to be difficult to get working. – uncoded Apr 13 '16 at 02:51
  • I believe the issue your running into is associated with the parent container consuming the child bound mouse events. – Jonny Henly Apr 13 '16 at 03:00
  • Is there anything I can do to solve this problem? Would the only way be adding the mouse listener to the JPanel instead? – uncoded Apr 13 '16 at 03:09
  • I'm not too sure as I've never tried anything like your scenario, however I don't think you'd want to listen for mouse events on the JPanel and then pass them to the Applet. Did you read trashgod's answer, in the link above, where he says "You may be able to leverage the hybrid approach shown in the examples cited [here](http://stackoverflow.com/a/12449949/230513)."? That might be a good approach. – Jonny Henly Apr 13 '16 at 03:17
  • Hmm, alright. Thanks for pointing me in the right direction, I'll give the hybrid approach a chance and see if I can get it to work. – uncoded Apr 13 '16 at 03:34
  • *"it is necessary for the certain thing I am doing."* Which is what, exactly? Applets are the best solution for an extraordinarily small number of things. What is your extraordinary thing? – Andrew Thompson Apr 14 '16 at 02:04
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. (AKA don't mix AWT and Swing components - for Swing use a `JApplet`.) – Andrew Thompson Apr 14 '16 at 02:06
  • See also [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/entry/moving_to_a_plugin_free). – Andrew Thompson Apr 14 '16 at 02:06

0 Answers0