Reputation: 41
Anyone have another view for this issue?
I need assistance for this part of the code, I would like to perform an actionListener (Add white border to the Jbutton pressed) only ONCE and proceed to another actionListener (Add blue border to another (different)Jbutton pressed). Here's my code which is only performing the white border continuously. Your feedback is much appreciated.
for(int c = 0; c< 10; c++)
{
for (int r = 0; r< 10; r++)
{
bu1[c][r] = new JButton(); //Insert Into List
panel.add(bu1[c][r]);
final int i = c;
final int j = r;
bu1[i][j].addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bu1[i][j])
{
bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
bu1[i][j].removeActionListener(this);
}
}
});
bu1[i][j].addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bu1[i][j])
{
bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
}
}
});
Upvotes: 0
Views: 295
Reputation: 201439
If I understand your question, then the simplest solution is to keep a count
in your ActionListener
. Because then you only need one ActionListener
with something like,
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
JButton obj = bu1[i][j]; // <-- save the typing.
if (e.getSource() == obj) {
if (count == 0) {
obj.setBorder(BorderFactory.createLineBorder(
Color.WHITE, 2)); // <-- set to white on 0.
} else if (count == 1) {
obj.setBorder(BorderFactory.createLineBorder(
Color.BLUE, 2)); // <-- set to blue on 1.
}
count++;
}
}
Upvotes: 2