Hi i need to change JLabel which is on JFrame, from JPanel. I can change this only when the mouse is moving, but i need to change everytime when score++;
private void panelKwadraty1MouseMoved(java.awt.event.MouseEvent evt)
{
jLabel1.setText("Twoj wynik to: "+panelKwadraty1.getScore());
}
I need to change when:
if (kwadrat[i].sprawdzKolizje(belka) == 1)
{
kwadrat[i]=new Kwadrat(kwadrat[i].getKolor());
score++;
// Jframe.jLabel1.setText("Your score is :" + score); <--- i need do that
}
edit:
I solved problem. I used propertyChangeListener.
In JFrame:
panelKwadraty1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
panelKwadraty1PropertyChange(evt);
}
});
private void panelKwadraty1PropertyChange(java.beans.PropertyChangeEvent evt)
{
if(evt.getPropertyName().equals(PanelKwadraty.ZMIANA_WYNIKU)){
setLabelText(""+evt.getNewValue());
}
}
In JPanel:
int old_score = score;
score++;
this.firePropertyChange(ZMIANA_WYNIKU, old_score, score);
From the code you have provided I assumed that the jLabel1 is public since you are calling it within a method of the panel.
Thus I think that your problem here might be that there are to many changes when you move the mouse and the label is not being repainted quickly enough. To make sure its repaint happens ASAP use code below:
Hope that was it.