- I created three JLabels(one,two,three) and add a mouselistener to it.
- For every even turns, the program will allow me to click any of the JLabels once and print out the JLabel I have clicked.
My program is throwing me the following errors
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.awt.AWTEventMulticaster.addInternal(AWTEventMulticaster.java:874)
at java.awt.AWTEventMulticaster.add(AWTEventMulticaster.java:565)
at java.awt.Component.addMouseListener(Component.java:5572)
at PageOne.selectJLabel(PageOne.java:48)
at PageOne.<init>(PageOne.java:30)
at PageOne$1.run(PageOne.java:73)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I reviewed my code again and it seems that my program will just add the mouseListener
in the infinite loop till it runs out of memory.
I sat down and rethink about how can I change my codes to fit in my above requirements.
but I seriously can't think of anything and needed help.
my code
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PageOne extends JFrame {
private JLabel[] jLabel = {new JLabel("one"), new JLabel("two"), new JLabel("three")};
private JPanel panel = new JPanel(new FlowLayout());
boolean isLooping;
boolean clicked;
int ctr;
public PageOne() {
ctr = 0;
clicked = false;
isLooping = false;
for(int i = 0; i < 3; i++) {
panel.add(jLabel[i]);
}
while(!isLooping) {
if(ctr%2 == 0) {
selectJLabel();
}
else {
}
ctr++;
}
add(panel);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setSize(800,800);
setLocationRelativeTo(null);
setVisible(true);
}
public void selectJLabel() {
for(int i = 0; i < 3; i++) {
jLabel[i].addMouseListener(new LabelListener());
}
}
public class LabelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
if(clicked) {
return;
}
JLabel label = (JLabel) e.getSource();
for(int i = 0; i< 2; i++) {
if(label == jLabel[i]) {
System.out.println("JLabel" + i + " was clicked");
clicked = true;
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PageOne();
}
});
}
}