0

Sorry for the bad title, I couldn't think of a better way to phrase it.

Anyway, I need my JLabel to have a different MouseListener when there are more than 2 objects in the same container. I'm trying to make a calendar program so there are 42 panels that these labels are added to. When there are too many labels, I want the last one to be able to open a window that will show the rest.

Right now, when there is more than 2 labels, the last label has both the mouseListener from inside the if (number_of_labels[index-7]) statement and from the if (mouseListenerActive) statement.

This method is called in a loop elsewhere. If you need to see anything else, I'll add it.

public static void insertLabel(String text, final int index, Color colour) {
    final JLabel label = new JLabel();

    label.setText(text);
    label.setOpaque(true);
    label.setBackground(colour);

    mouseListenerActive = true;

    if (number_of_labels[index-7] == 2) {
        label.setBackground(Color.RED);
        JLabel last_label = (JLabel) calendar_boxes[index].getComponent(2);
        last_label.setText("     ▼");
        last_label.setForeground(Color.WHITE);
        last_label.setBackground(Color.BLACK);

        mouseListenerActive = false;
        last_label.addMouseListener(new MouseListener() {
            @Override public void mouseExited(MouseEvent e) {}
            @Override public void mouseEntered(MouseEvent e) {}
            @Override public void mouseReleased(MouseEvent e) {}
            @Override public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {
                //int day = index - (position of last day - number of days in current month)
                int day = index - (Integer.parseInt(monthDataNode.getChildNodes().item(Main.year-1900).getChildNodes().item(Main.month_index-1).getTextContent()) - Constants.month_lengths[Main.month_index-1]);
                calendarList.open(day, Main.month_index-1, Main.year);
            }
        });
    } else if (number_of_labels[index-7] > 2) {
        return;
    }

    if (mouseListenerActive) {
        label.addMouseListener(new MouseListener() {
            @Override public void mouseExited(MouseEvent e) {}
            @Override public void mouseEntered(MouseEvent e) {}
            @Override public void mouseReleased(MouseEvent e) {}
            @Override public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {
                //int day = index - (position of last day - number of days in current month)
                int day = index - (Integer.parseInt(monthDataNode.getChildNodes().item(Main.year-1900).getChildNodes().item(Main.month_index-1).getTextContent()) - Constants.month_lengths[Main.month_index-1]);
                calendarEdit.open(day, Main.month_index-1, Main.year, label.getText());
            }
        });
    }

    calendar_boxes[index].add(label, new AbsoluteConstraints(19, 6+(15*number_of_labels[index-7]), 40, 12));
    number_of_labels[index-7]++;
}
alexanderd5398
  • 332
  • 1
  • 4
  • 16
  • what is the purpose of this block of code `if (mouseListenerActive) { ... }`. Have you look into any Java tutorial? There is an excellent tutorial on the [Java Oracle](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) website. – hfontanez Jan 18 '15 at 20:37
  • That was me trying to stop the label from getting that MouseListener added to it if the other one was already. mouseListenerActive is set to false when the other one is added and it'll be true if it's not. Also I know how to use MouseListeners. I don't see how a tutorial would help. – alexanderd5398 Jan 18 '15 at 20:43
  • What's the problem with this? Is it that when you add the alternative MouseListener, the original one is still there and working? You need to add the behaviour you see and the behaviour you'd like to see... – J Richard Snape Jan 18 '15 at 21:00
  • I've said it: "Right now, when there is more than 2 labels, the last label has both the mouseListener from inside the if (number_of_labels[index-7]) statement and from the if (mouseListenerActive) statement." So yeah, the original is still there, sorry if it wasn't clear. – alexanderd5398 Jan 18 '15 at 21:01
  • OK - yeah - I thought you meant both were being added in the same passage through the code, but I dont think you do. Have a look at accepted answer to this question http://stackoverflow.com/questions/13363865/removing-mouselistener-from-a-jlabel - you need to remove the original mouselistener when you add the new one, I think. If you need more detail - I'll add an answer. – J Richard Snape Jan 18 '15 at 21:03
  • I tried removing the listener in the event and just before the line `last_label.addMouseListener(new MouseListener() {` in the first if statement and it wouldn't work. I can add the code from that too if you want. – alexanderd5398 Jan 18 '15 at 21:06
  • Oh. Didn't work? What does that mean here? Wouldn't compile? Or seemed to leave the listener intact? I wonder - are you adding a listener every time you (e.g.) change month. Could you have lots and lots of listeners? – J Richard Snape Jan 18 '15 at 21:08
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69082/discussion-between-j-richard-snape-and-alexanderd5398). – J Richard Snape Jan 18 '15 at 21:11

1 Answers1

1

In your code, before you add the second MouseListener, remove the first. As you are using anonymous classes and don't have a reference to the original MouseListener, use the following:

MouseListener existingListener = last_label.getMouseListeners()[0];
last_label.removeMouseListener(existingListener);
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79