I've searched through all the other issues with this, but with no luck. I have been trying different things for over four hours and have gotten absolutely no progress (or errors, which is even more frustrating) so I figured I would finally ask here.
I am working on a small survival game as a side project to teach myself more about Java UI elements. The player scavenges for food and such, and it is a race against starvation and thirst. The situation I have is such:
The game opens into a controller (MainController) that has several bars indicating the character's stats; for this example, we'll say Hunger and Thirst.
The player can click a button to open the character's inventory, which includes their food. This opens a new window on a new controller (InventoryController) that lists buttons corresponding to the character's inventory items.
When the player clicks a food item, the character's Hunger drops, and the associated ProgressBar should drop as well.
Issue is: The character's internal hunger stat drops, and the ProgressBar's progress drops, but the bar never updates visually, even though it did internally.
As a brief example:
public class MainController implements Initializable {
@FXML private ProgressBar hungerBar;
public void initialize(URL url, ResourceBundle rb) {
updateBars();
}
public void updateBars(){
hungerBar.setProgress(CC.pc.getHunger()/100);
}
}
This works at first. It successfully get's the character's hunger, divides it by 100, and then sets the bar (in this case, to .40). No issues. The bar appears and is 40% full. However, when I open and use the second Controller, nothing happens.
public class InventoryController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML private void feedVenison(ActionEvent event) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
loader.load();
PlayController c = loader.<PlayController>getController();
//Stuff that does the math and changes the character's stats here;
//cut short for clarity.
c.updateBars();
}
}
I can add in a few Print commands, such as a System.out.println(hungerBar.getProgress()), and see that everything functions perfectly fine. The Character's hunger decreases, and in turn, the bar's progress is updated and is, functionally, lower. However, the bar never changes, no matter where between .01 and 1 it is set.
I've tried and tried and I'm at my wits end! I'm obviously doing something wrong, and I'm sure it is obvious, but I have no idea what. There's no errors, it just doesn't work. Am I just using the wrong tool for this job? What gives?
Update 1:
More digging brought me to this question, which is very similar to what I am trying to do. Unfortunately, it has no answers either. It has a comment that leads to this Oracle doc, which I attempted to implement.
public void updateBars(){
DoubleProperty hunger = new SimpleDoubleProperty(CC.pc.getHunger());
hungerBar.progressProperty().bind(hunger);
}
But once again, I get the exact same issue. The bar updates internally, yet the UI never changes and always displays a static amount that is incorrect. What do I do?
Update 2:
I keep digging, but every time I think I have found the answer it just does the exact same thing over and over again. Tried out this, but same result.
public void updateBars(){
maybeWillWork();
}
public void maybeWillWork()
{
Platform.runLater(new Runnable() {
@Override public void run() {
hungerBar.setProgress(CC.pc.getHunger()/100);
System.out.println("It ran!");
}
});
}
Exact same result.
Please. What am I doing wrong? I refuse to believe that the entirety of StackOverflow is stumped by a simple ProgressBar like I am; someone has to know what stupid mistake I am making.
Update 3:
Well that was frustrating, but I got it to work. I have posted the answer below.