Ti Ringrazio per la precedente risposta
in pratica ho un JPanel che ha come layout un GridLayout a questo JPanel aggiungo che due for annidati tanti JPanel cosi da formare una matrice di JPanel (e ogni uno di questi ha un MouseListener associato ) e prorio come hai capito anche tu dovrei fare in modo che ne puo essere "acceso " solo uno....ho capito la soluzione che mi hai suggerito ma non so come implementarla !!
/**qui creo la matrice di JPanel (Posto panel estende JCompenent)
int count =0;
for (int row = 0; row < h; row++) {
for (int column = 0; column < b; column++) {
if((row>=x && row<=x1) && (column>=y && column<=y1))
{PostoPanel prato = new PostoPanel(); panel.add(prato); }
else {
PostoPanel pixelPanel = new PostoPanel(count);
pixelPanel.addMouseListener(new PostoListener(pixelPanel));
panel.add(pixelPanel);
count++;
}
}
}
/**questo è il listener
class PostoListener implements MouseListener {
private PostoPanel panel;
public PostoListener(PostoPanel p) {
panel = p;
}
public void mousePressed(MouseEvent event) {
lpos.setText(""+ panel.geID());
System.out.println(""+lpos.getText());
panel.setBackgroundColor(Color.YELLOW);
panel.repaint();
//System.out.println("I was clicked.");
//System.out.println(""+ panel.geID());
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
}
/**questo è Posto Panel
public class PostoPanel extends JComponent {
private static final int PIXEL_SIZE = 17;
private Color backgroundColor;
private int id;
public PostoPanel(int i) {
id =i;
this.backgroundColor = Color.white;
this.setBorder(BorderFactory.createLineBorder(Colo r.BLACK));
this.setPreferredSize(new Dimension(PIXEL_SIZE, PIXEL_SIZE));
}
public PostoPanel() {
this.backgroundColor = Color.GREEN;
this.setBorder(BorderFactory.createLineBorder(Colo r.black));
this.setPreferredSize(new Dimension(PIXEL_SIZE, PIXEL_SIZE));
}
public Color getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
public int geID(){
return id;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
}
}**/