user2542424
user2542424

Reputation: 11

GUI not updating dynamically

im currently trying to create a program that dynamically changes the text on JTextArea's and JButtons when they are pressed, or display a JOptionPane. At the moment nothing is happening when i press the buttons, they are not getting updated nor are they making a dialogue box appear.

Help is appreciated private MillionaireGui view; private Millionaire model; private String question; private int gameState; private boolean isRunning;

public MillionaireController(MillionaireGui view, Millionaire model) {
    this.view = view;
    this.model = model;
    this.question = null;
    this.gameState = 0;
    this.isRunning = true;
}

public void setModel(Millionaire model) {
    this.model = model;
}

public void setView(MillionaireGui gui) {
    this.view = gui;
}

public void getQuestion() {
    question = model.getDeck().generateQuestion();
    view.setQuestion(question);
}
public void update(){
 while(isRunning){

 if(gameState == 0){

    getQuestion();

    ArrayList<String> ans = model.getDeck().getAnswers();
    view.setButtonA(ans.get(0));
    view.setButtonB(ans.get(1));
    view.setButtonC(ans.get(2));
    view.setButtonD(ans.get(3));
    gameState = 1;

}

if(gameState == 1){

    if(view.getAnswer() != 0){
        if(model.getDeck().isCorrect(view.getAnswer())){
            view.dispCorrectAnswer();
            view.setAnswer(0);
                gameState = 0;
        }
        else {
            gameState = 3;
        }
    }

  }
  if(gameState == 3){
      isRunning = false;
      view.displayErrorMsg();
    }
}
}
@Override
public void run() {
  update();
}

GUI:

public void setButtonB(String str){
    buttonB.setText(str);
}

public void setButtonC(String str){
    buttonC.setText(str);
}

public void setButtonD(String str){
    buttonD.setText(str);
}

public void setAnswer(int num){
    answer = num;
}

public String getQuestion(){
   return question;

}

public void setQuestion(String str){
   question = str;
   questionField.setText(str);
}

MAIN:

public class Millionaire_main {

public Millionaire_main(){

}

public static void main(String[] args) {
    MillionaireGui gui = new MillionaireGui();
    QuestionDeck deck = new QuestionDeck();
    Millionaire model = new Millionaire(deck);
    MillionaireController control = new MillionaireController(gui, model);   
    gui.setVisible(true);

    Thread thread = new Thread(control);
    thread.start();
  }
}

Upvotes: 0

Views: 2073

Answers (2)

blazetopher
blazetopher

Reputation: 1050

Looks like maybe you just need to revalidate the container.

After you set all the button text fields, call gui.revalidate() to mark everything as invalid & validate. Here's more on the differences between those 3 methods

Also (as mentioned by @npinti) - I'm not sure exactly what you're doing with the extra thread, but be aware that modifying GUI components outside of the AWT thread is NOT a good idea

Upvotes: 1

npinti
npinti

Reputation: 52205

The code within the update() method seems to be running with a thread. What I think is happening is that you have 2 threads, one of which is doing some background task which causes the update. The background thread is not the EDT, thus any UI updates will not be visible.

Trying the approach below would fix the problem (most likely at least)

SwingUtilities.invokeLater(new Runnable() 
{
    @Override
    public void run() {
        view.setButtonA(ans.get(0));
        view.setButtonB(ans.get(1));
        view.setButtonC(ans.get(2));
        view.setButtonD(ans.get(3));
    }
});

The above should place your button settings events on the EDT, which should trigger the change.

Upvotes: 1

Related Questions