I am using JProgressBar
as a health bar, so I want to manually set the bars maximum and value as the game progresses. The game is a Pokemon game and it allows for new Pokemon to be summoned. These new Pokemon can have different max health values and current health values which is why I call progressBar.setMaximum(newPokemonMaxHealth)
and progressBar.setValue(currentPokemonHealth)
. I even call progressBar.repaint()
, progressBar.revalidate()
, and progressBar.update(progressBar.getGraphics()
, and none of those have worked for me.
Here is the initialization of the progress bar:
UIManager.put("ProgressBar.selectionForeground", Color.BLACK);
poke1Health = new JProgressBar(0);
poke1Health.setMaximum(board.getTrainer1().getBattlingPokemon().getMaxHealth());
poke1Health.setValue(board.getTrainer1().getBattlingPokemon().getCurrentHealth());
poke1Health.setStringPainted(true);
poke1Health.setForeground(Color.green);
poke1Health.setBackground(Color.red);
poke1Health.setPreferredSize(new Dimension(50, 10));
centerPanel1.add(poke1Health);
Then when I attempt to change the maximum and value:
board.getTrainer1().pokemonChanged(1);
poke1Health.setValue(board.getTrainer1().getPokemon(0).getCurrentHealth());
poke1Health.setMaximum(board.getTrainer1().getPokemon(0).getMaxHealth());
poke1Health.repaint();
poke1Health.revalidate();
poke1Health.update(poke1Health.getGraphics());
I printed out the values of the current health and max health of the different pokemon and the values were what they were supposed to be, which shows the values are not the problem.
For some reason, the bars value does not seem to want to change but the maximum does.
When the new Pokemon health is higher than the previous Pokemon health, it will not update the value. If the new Pokemon health is lower than the previous Pokemon health, it will update the value, no problem. Here is what I printed:
Pokemon 1 Max Health: 49
Pokemon 2 Max Health: 50
Pokemon 1 Current Health: 49
Pokemon 2 Current Health: 50
Then I printed the bars value before and after I set the new maximum and the new value.
Bars value before: 49
Bars value after: 49
Bars Max before: 49
Bars Max after: 50
For some reason the Maximum value changed, but the value doesn't change. The print out I did to get Pokemon 2 current health is the same exact code I used to attempt to set the new value of the bar. Here:
poke1Health.setValue(board.getTrainer1().getBattlingPokemon().getCurrentHealth());
System.out.println("Pokemon 2 Current Health: " + board.getTrainer1().getBattlingPokemon().getCurrentHealth());
Please help me out here! I can't find out why the progress bar value does not want to update!!!
Thank you!