I’m new to GUI programming and I tried out some code and wanted to constantly change the background of a JPanel called “HeaderPanel”.
Why isnt this working as I wished? (Color stays the same…)
private void changeColors() {
int r = 0;
int g = 155;
int b = 12;
while(true) {
r = (r+1)%255;
g = (g+1)%255;
b = (b+1)%255;
Color color = new Color(r,g,b);
HeaderPanel.setBackground(color);
}
}
Your while loop is stopping the repaint manager from ever getting around to changing the colors.
You need to, some how, execute the request in the background, something like this
You might like to read up on
UPDATED with Extended Example