Skip to content Skip to sidebar Skip to footer

AddMouseListener To A Program

im trying to overwrite mouseClicked function of MouseAdapter then i can use it in my program as follow: import java.awt.Component; import java.awt.event.MouseAdapter; import java.a

Solution 1:

This code (is an SSCCE &) works. Try changing it one line at a time until you see the error manifest.

import java.awt.event.*;
import javax.swing.*;
import java.net.URL;

public  class test {

    public static void main(String[] args) throws Exception {

        String uri = "http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/MouseAdapter.html";
        final URL url = new URL(uri);
        JEditorPane htmlPanel = new JEditorPane(url);
        JFrame frame = new JFrame();
        frame.setContentPane(htmlPanel);

        htmlPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("adr is equal to" + url);

                System.out.println("Clicked!" + e);
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 800);
        frame.setVisible(true);
    }
}

Update

..seems bug is from htmlpanel

I did a bit of digging, based on the comment of StanislavL. It seems a SimpleBrowserFrame extends HtmlPanel & provides the method getComponent() which..

Gets the component that renders the frame. ..

Perhaps if a SimpleBrowser was used, you could add the listener to the child and it would work. Admittedly I am just guessing after a quick trawl though the docs.


Solution 2:

Just a guess. Check children components of the htmlPanel. May be they cover whole panel. Try to attach the listener to all subcomponents after the HtmlPanel is filled with content.


Post a Comment for "AddMouseListener To A Program"