It seems it's time for me to begin another round of Java questions. So without further ado...I'm trying to associate mouse listeners with dynamically created JButtons. The problem is I want the event handlers to affect the JButton that was clicked. I.E. when a button is clicked I want it to move 50 pixels higher on the screen.
When I try this I can't get around errors similar to "Cannot use non-final local variable from a different method." Here is what I believe to be the relevant portion of the code.
The setLocation, getX, getY are the problems.
Let me know if any additional information would be helpful.
Thanks,
Brian
When I try this I can't get around errors similar to "Cannot use non-final local variable from a different method." Here is what I believe to be the relevant portion of the code.
Code:
public void displayCard(Card aCard, int i)
{
Insets i1 = new Insets(0, 0, 0, 0);
ImageIcon cardImage = new ImageIcon(aCard.getGraphic());
JButton button = new JButton(cardImage);
button.setMargin(i1);
centerPane.add(button);
button.setSize(button.getPreferredSize());
button.setLocation(225 + i*50, 475);
aCard.setCard(button);
aCard.getCard().addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
aCard.getCard().setLocation(aCard.getCard().getX() - 50, aCard.getCard().getY() - 50);
}
}
);
}
The setLocation, getX, getY are the problems.
Let me know if any additional information would be helpful.
Thanks,
Brian