ADAPTER CLASSES WHICH LET US USE METHODS THAT WE WANT WITHOUT NEEDING TO OVERRIDE THEM ALL
import javax.swing.*;
class First{
public static void main(String args[]){
Second gui = new Second();
gui.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
gui.setSize(300, 200);
gui.setVisible(true);
}
}
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
class Second extends JFrame {
private String details;
private JLabel statusBar;
public Second(){
super("Title");
statusBar = new JLabel("Default");
add(statusBar, BorderLayout.NORTH);
// Adding a mouse listener and passing the MouseClass object
addMouseListener(new MouseClass());
}
// Creating the MouseClass class which extends MouseAdapter
private class MouseClass extends MouseAdapter{
// Setting the events
public void mouseClicked(MouseEvent e){
details = String.format("Clicked %d times ", e.getClickCount());
// Checking which button has been clicked and setting the statusBar string to something new
if(e.getButton() == MouseEvent.BUTTON1){
details += "with the left mouse button";
} else if (e.getButton() == MouseEvent.BUTTON2){
details += "with the scroll wheel";
} else if (e.getButton() == MouseEvent.BUTTON3){
details += "with the right mouse button";
}
statusBar.setText(details);
}
}
}