Reputation: 627
I have a game frame, with an array of JLabels in it, that i added existing (extended) MouseListener for two of them (the last and the first) at start. Now, every time a JLabel with a MouseListener had been clicked, i want it "not have" that MouseListener anymore, and instead, the MouseListener goes to the next/previous index in the JLabel array.
I can't find a mechanism that can handle that algorithm.
public class NumberGameFrame extends javax.swing.JFrame {
...
JLabels[] numbers;
int left, right;
public void playPVC() {
...
left = 0; right = numbers.legth - 1;
PVCMouse pvc_mouse = new PVCMouse(); // MouseListener
setPlaybleNumbers(left,right,pvc_mouse);
...
}
public void setPlaybleNumbers(int left, int right, MouseListener mouse){
for(int i = 0; i < numbers.length; i++){ // "kill" other numbers first
if (i != left && i != right){
if (numbers[i].getMouseListeners() != null){
numbers[i].removeMouseListener(mouse);
}
...
}
}
numbers[left].addMouseListener(mouse);
...
numbers[right].addMouseListener(mouse);
...
}
...
}
My thought was that some how pvc_mouse.mouseClicked() can invoke this.setPlaybleNumbers(++left,..) or (--right,...), but the MouseListener cant handle the JLabel array and the setPlaybleNumbers method because they are not static.
Upvotes: 1
Views: 133
Reputation: 15434
I see 2 solutions:
PVCMouse
inner class of NumberGameFrame
. It will allow you to use all non static methods and variables.PVCMouse
so it can use it by itself without knowing about NumberGameFrame
. In this case you'll have to move left
and right
variables inside PVCMouse
. Upvotes: 1