2

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!

Cole
  • 51
  • 9
  • 1
    You will probably have best luck getting a decent answer if you post [mcve] code with your question -- a small compilable runnable example program that demonstrates your problem for us. This isn't your whole program, and again isn't large, but is small, small enough to post as code-formatted text in its entirety in your question. Again it runs -- has a main method, has imports and all that is needed to compile. – Hovercraft Full Of Eels Dec 05 '16 at 23:26
  • The actual code is very large! I tried this: `import java.awt.FlowLayout; import javax.swing.JProgressBar; public class JProgressBarError{ public static void main(String args[]) { JProgressBar bar = new JProgressBar(0, 10); bar.setValue(10); System.out.println(bar.getValue()); System.out.println(bar.getMaximum()); bar.setMaximum(20); bar.setValue(20); System.out.println(bar.getValue()); System.out.println(bar.getMaximum()); } }` This worked and the value was able to change, but for some reason it is not working in my code! – Cole Dec 05 '16 at 23:33
  • I have no idea how to go to new line in comments!!!! :( – Cole Dec 05 '16 at 23:39
  • 1
    Again, **we don't want to see the actual full code**. Again, I'm asking you to consider creating a new **small** compilable and runnable program. Please read the link. Also avoid posting code in links because as you're finding, it can't be formatted. Also a "side" recommendation, one unrelated to your problem is that you appear to have some "train-wreck" code with large method chains. That can make for risky brittle code, and you may wish to refactor it some. Please see [this link](http://wiki.c2.com/?TrainWreck) for more on this. – Hovercraft Full Of Eels Dec 05 '16 at 23:42
  • @HovercraftFullOfEels Wow, Simplifying the code and writing what I did before showed me that I have to setMaximum before I setValue if the new value is higher than the current Maximum. Problem solved. Thank you! Been trying to figure this out way too long..... – Cole Dec 05 '16 at 23:42
  • Good deal -- consider posting that as an answer to your question. – Hovercraft Full Of Eels Dec 05 '16 at 23:43
  • @HovercraftFullOfEels Thank you for the "train-wreck" code. I completely understand that and is actually great to know. This project is a group project, so most of that code is written by others, I just have to access it! Making a method in the actual gameboard would help reduce this "train wreck" code and is something I will change! – Cole Dec 05 '16 at 23:48

2 Answers2

1

Turns out if your new value is higher than the bars maximum value, it won't set the new value. You must first update the maximum value then set the new value that is equal to or lower than the maximum value. Instead, I put setMaximum before setValue and everything worked!

Cole
  • 51
  • 9
0
import java.awt.FlowLayout;
import javax.swing.JProgressBar;

public class JProgressBarError {
    public static void main(String args[]) {
        JProgressBar bar = new JProgressBar(0, 10);
        bar.setValue(4);
        System.out.println(bar.getValue());
        System.out.println(bar.getMaximum());
        bar.setMaximum(20);
        bar.setValue(17);
        System.out.println(bar.getValue());
        System.out.println(bar.getMaximum());
    }
}

I copied your example in the comment and everything works. If you start from this minimal example, should notice there are no problem. Maybe are the colors of your components the cause? Again, I suggest you to start from an atomic example.

Naialeoque
  • 93
  • 1
  • 4