I have a GUI with about 26 different labels, but would like to be able to execute the same code for all of them when clicked, so I figured I would use a MouseListener and mouseClicked method to execute the code. The problem I'm having is that when I click on any of the labels, I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JRootPane cannot be cast to javax.swing.JLabel
at dealornodeal.ui.DealOrNoDealUI.mouseClicked(DealOrNoDealUI.java:628)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I can see that the mouseClicked() method is returning a JRootPane component instead of a JLabel component, and a JRootPane apparently can't be cast to a JLabel. My question is, how to I get the JLabel component when using the mouseClicked() method, instead of a JRootPane?
Here is my code where I'm adding a MouseListener to all the JLabels and then trying to execute code on mouseClicked():
public DealOrNoDealUI() {
initComponents();
this.setLocationRelativeTo(null);
this.components = this.getComponents();
for(Component component : this.components){
component.addMouseListener(this);
}
}
public void mouseClicked(MouseEvent e) {
Component clickedComponent = e.getComponent();
JLabel clickedLabel = (JLabel) clickedComponent;
if(this.game.getPlayer().getPlayerCase() == null){
for(Case gameCase : this.game.getCases()){
if(gameCase.getCaseNumber() == Integer.parseInt(clickedLabel.getText())){
this.game.getPlayer().setPlayerCase(gameCase);
}
}
}
}
I'm using a null layout for the GUI.