1. I created three JLabels(one,two,three) and add a mouselistener to it.
  2. 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++) {

        while(!isLooping) {
            if(ctr%2 == 0) {
            else {



    public void selectJLabel() {
        for(int i = 0; i < 3; i++) {
            jLabel[i].addMouseListener(new LabelListener());

    public class LabelListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
             if(clicked) {
            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() {
            public void run() {
                new PageOne();
2 Years
Discussion Span
Last Post by johnson_2

The loop at line 26 keeps calling selectJLabel.
selectJLabel creates a new instance of the LabelListener class each time it's called.
So you keep creating new instances until you run out of memory.

There isn't enough info here to say how the program should be re-structured, but the while loop at 26 looks very wrong to me, as does adding a listener more than once.

Perhaps you culd explain the requirement a bit more fully?

Edited by JamesCherrill


the requirements is just for every even turns. (0,2,4,6,8,10)etc..
I am allowed to click on any one of the three JLabels(one,two,three) once and the console
will print out the JLabel that I have clicked.

for example

turn 0: I am allowed to click once, If I clicked on JLabel 1, the console will print out "JLabel 1 was clicked"
the turn is incremented to 1.

turn 1: since I wrote nothing on the else block. then for now the turn is incremented to 2

turn 2: I am allowed to click once, if I clicked on JLabel 2, the console will print out "JLabel 2 was clicked"
the turn is incremented to 3.

and so on..

Edited by johnson_2


If this was a console app you would have a loop to control that, but for a Swing event-driven app you don't have that loop at all.
Just add the listener(s) to the buttons.
In the actionPerformed you do whatever, and increment the turn.
Then you just do nothing - Swing will call you again when there is an event for you to handle.
The problem here is that you don't have anything (yet?) to generate an event for the odd turns, so you never get to turn 2.
Maybe you can do some trivial place-holder for the odd turns (eg a single button) that will increment the turn counter to the next even number?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.