Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

mouselistener in java

package com.javapointers.javase;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
 
public class MouseListenerTest implements MouseListener {
 
    JPanel leftPanel, rightPanel;
    JTextArea textArea;
    JFrame frame;
 
    public MouseListenerTest() {
        frame = new JFrame("MouseListener Test");
        textArea = new JTextArea();
        textArea.setLineWrap(true);
 
        leftPanel = new JPanel();
        rightPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(200, 100));
        rightPanel.setPreferredSize(new Dimension(200, 100));
        leftPanel.setBackground(Color.GREEN);
        rightPanel.setBackground(Color.BLUE);
 
        leftPanel.addMouseListener(this);
        rightPanel.addMouseListener(this);
 
        frame.setLayout(new BorderLayout());
        frame.add(leftPanel, BorderLayout.WEST);
        frame.add(rightPanel, BorderLayout.EAST);
        frame.add(textArea, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have clicked the left panel");
        } else {
            textArea.setText("You have clicked the right panel");
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have pressed the left panel");
        } else {
            textArea.setText("You have pressed the right panel");
        }
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have released mouse on left panel");
        } else {
            textArea.setText("You have released mouse on right panel");
        }
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have entered mouse on left panel");
        } else {
            textArea.setText("You have entered mouse on right panel");
        }
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
        if (e.getSource() == leftPanel) {
            textArea.setText("You have exited mouse on left panel");
        } else {
            textArea.setText("You have exited mouse on right panel");
        }
    }
 
    public static void main(String args[]) {
        MouseListenerTest test = new MouseListenerTest();
    }
}
Comment

mouselistener in java

// Java program to handle MouseListener events
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Mouse extends Frame implements MouseListener {
 
    // Jlabels to display the actions of events of mouseListener
    // static JLabel label1, label2, label3;
 
    // default constructor
    Mouse()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a frame
        JFrame f = new JFrame("MouseListener");
 
        // set the size of the frame
        f.setSize(600, 100);
 
        // close the frame when close button is pressed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create anew panel
        JPanel p = new JPanel();
 
        // set the layout of the panel
        p.setLayout(new FlowLayout());
 
        // initialize the labels
        label1 = new JLabel("no event  ");
 
        label2 = new JLabel("no event  ");
 
        label3 = new JLabel("no event  ");
 
        // create an object of mouse class
        Mouse m = new Mouse();
 
        // add mouseListener to the frame
        f.addMouseListener(m);
 
        // add labels to the panel
        p.add(label1);
        p.add(label2);
        p.add(label3);
 
        // add panel to the frame
        f.add(p);
 
        f.show();
    }
 
    // getX() and getY() functions return the
    // x and y coordinates of the current
    // mouse position
    // getClickCount() returns the number of
    // quick consecutive clicks made by the user
 
    // this function is invoked when the mouse is pressed
    public void mousePressed(MouseEvent e)
    {
 
        // show the point where the user pressed the mouse
        label1.setText("mouse pressed at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse is released
    public void mouseReleased(MouseEvent e)
    {
 
        // show the point where the user released the mouse click
        label1.setText("mouse released at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse exits the component
    public void mouseExited(MouseEvent e)
    {
 
        // show the point through which the mouse exited the frame
        label2.setText("mouse exited through point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse enters the component
    public void mouseEntered(MouseEvent e)
    {
 
        // show the point through which the mouse entered the frame
        label2.setText("mouse entered at point:"
                       + e.getX() + " " + e.getY());
    }
 
    // this function is invoked when the mouse is pressed or released
    public void mouseClicked(MouseEvent e)
    {
 
        // getClickCount gives the number of quick,
        // consecutive clicks made by the user
        // show the point where the mouse is i.e
        // the x and y coordinates
        label3.setText("mouse clicked at point:"
                       + e.getX() + " "
                       + e.getY() + "mouse clicked :" + e.getClickCount());
    }
}
Comment

mouselistener in java

this.setLayout(new BorderLayout());
  checkBox = new JCheckBox();
  add(checkBox, BorderLayout.CENTER);
  setOpaque(false);
this.addMouseListener(new MouseListener() {
  public void mouseClicked(MouseEvent arg0) {
    TreePath tp = selfPointer.getPathForLocation(arg0.getX(), arg0.getY());
Comment

mouselistener in java

final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
final JToolBar toolBar = new JToolBar();
button.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    popup.show(e.getComponent(), e.getX(), e.getY());
toolBar.add(button);
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Comment

PREVIOUS NEXT
Code Example
Java :: while loop in java 
Java :: java list to array 
Java :: java program to remove duplicate words in a string 
Java :: mono command to compile C# library code 
Java :: add element in the last in double linked list in java 
Java :: assert multiple junit 
Java :: java String revers 
Java :: how to initialize an empty array in java 
Java :: labelled break statement in Java 
Java :: first and last digit of a number in java 
Java :: How to use java 8 comparator 
Java :: add custom font to android paint object 
Java :: creating a menu in java 
Java :: in java how to compare two strings 
Java :: JsonArray get first Object 
Java :: java php object 
Java :: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection 
Java :: integer to binary java 
Java :: reverse number java 
Java :: filter and map multiple fields from java stream 
Java :: how to get input of local date type java 
Java :: reversing the linked list 
Java :: How to read and write XML files 
Java :: long to int java 
Java :: how to convert string to int in java 
Java :: java character for end of file 
Java :: spring security logging 
Java :: copy linked list 
Java :: a?che les paramètres de la ligne de commande triés par ordre lexicographique en java 
Java :: How to merge two sorted arrays into a single sorted aggregate one? 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =