Night | Snow

handle mouse click events in Java

Posted on: December 19th, 2009

Tags: , , , , , ,

Here’s a little example on how to add a mouseListener to the component via addMouseListener.

      table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.NOBUTTON) {
                    System.out.println("No button clicked...");
                } else if (e.getButton() == MouseEvent.BUTTON1) {
                    System.out.println("Button 1 clicked...");
                } else if (e.getButton() == MouseEvent.BUTTON2) {
                    System.out.println("Button 2 clicked...");
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    System.out.println("Button 3 clicked...");
                }

                System.out.println("Number of click: " + e.getClickCount());
                System.out.println("Click position (X, Y):  " + e.getX() + ", " + e.getY());
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
        });

News

  • Facebook shares see modest debut

    Facebook shares end a volatile first day of trading at $38.23, barely above the company's initial pricing, having initially jumped more than 10%.

  • Met Police to extract phone data

    Mobile phone data of suspects in police custody is to be extracted and retained, regardless of whether charges are brought, the BBC has learned.

  • Silicon trick for next-gen memory

    Researchers reveal details of a promising way to make a fundamentally different kind of computer memory chip.

Martin Fischer said:

Client and partner of simra.ch

Wir können Simon Schärer als kompetenter und zuverlässiger Programmierer höchst weiterempfehlen. Er ist nicht nur ein guter Programmierer, sondern auch ein ausgezeichneter Designer.
Danke Developer’s Island

Follow us on Twitter

favorite

I get asked a lot - which SDK should we use for web programming? I recommend Netbeans

my toolbar

Simra GameMP3 playersubscribe to RSSMy delicious bookmarksFacebookTwitterE-mail

No Responses to “handle mouse click events in Java”

RSS Feed Icon  Subscribe to comments follow-up

Trackbacks/Pingbacks

Leave a reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest Tweet

loading...