I'm building a GUI Project in Java. In the parent class, I implemented MouseListener to highlight the button when the cursor hovers over it and stops when the cursor doesn't. I want to disable it in child class so that the button selected is then permanently highlighted showing that it was selected. In child class, when I hover over it, it again changes the color. I want to remove that specific part of the function from child class but I have to write it all over again to override the function. Is there any way I can disable that part in the child class?
Here's the code snippet
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
if (e.getSource() == notifications) {
notifications.setBackground(new Color(0x13A89E));
}
if (e.getSource() == attendance) {
attendance.setBackground(new Color(0x13A89E));
}
if (e.getSource() == marks) {
marks.setBackground(new Color(0x13A89E));
}
if (e.getSource() == learning) {
learning.setBackground(new Color(0x13A89E));
}
if (e.getSource() == assignments) {
assignments.setBackground(new Color(0x13A89E));
}
if (e.getSource() == GDB) {
GDB.setBackground(new Color(0x13A89E));
}
if (e.getSource() == MDB) {
MDB.setBackground(new Color(0x13A89E));
}
if (e.getSource() == quizzes) {
quizzes.setBackground(new Color(0x13A89E));
}
if (e.getSource() == lectureContents) {
lectureContents.setBackground(new Color(0x13A89E));
}
if (e.getSource() == courseInformation) {
courseInformation.setBackground(new Color(0x13A89E));
}
}
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
if (e.getSource() == notifications) {
notifications.setBackground(null);
}
if (e.getSource() == attendance) {
attendance.setBackground(null);
}
if (e.getSource() == marks) {
marks.setBackground(null);
}
if (e.getSource() == learning) {
learning.setBackground(null);
}
if (e.getSource() == assignments) {
assignments.setBackground(null);
}
if (e.getSource() == GDB) {
GDB.setBackground(null);
}
if (e.getSource() == MDB) {
MDB.setBackground(null);
}
if (e.getSource() == quizzes) {
quizzes.setBackground(null);
}
if (e.getSource() == lectureContents) {
lectureContents.setBackground(null);
}
if (e.getSource() == courseInformation) {
courseInformation.setBackground(null);
}
}
The methods of your parent class are public
I see no reason why you cannot simply override them and decide what you want to do:
Parent.java:
public class Parent implements MouseListener {
@Override
public void mouseEntered(MouseEvent e) {
// all your logic here to highlight when mouse entered/hovered
}
@Override
public void mouseExited(MouseEvent e) {
// all your logic here for clearing the highlight when the mouse has exited or is not hovering over the component
}
}
Child.java:
public class Child extends Parent {
@Override
public void mouseEntered(MouseEvent e) {
// we call super here as we want to inherit logic of the parent class for highlighting on mouse entered/hover
super.mouseEntered(e);
}
@Override
public void mouseExited(MouseEvent e) {
// we dont call super here as we don't want to inherit the parents behavior
}
}