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]++;
}