0

I'm building a Java program using the MVC/Observer pattern and have managed to get the update method in my View (Observer) class logging changes from my Model (Observable) class. The first 3 components below are JTextFields which are correctly being set to an updated String.

public void update(Observable o, Object arg) {

    textPlayer1.setText(model.getPlayerX().getName());
    textPlayer2.setText(model.getPlayerO().getName());
    textTurn.setText(model.determineTurn().getName());
    frame.repaint();

}

However, despite even calling repaint() on the containing frame, the GUI isn't being updated at all - the text fields displayed remain blank even though calling getText() shows the updated values. What's the problem here?

Extract from my View (Observer) class:

public Connect4View(Connect4Game model, Connect4Controller controller) {

    this.model = model;
    model.addObserver(this);
    this.controller = controller;
    createGameWindow();
    controller.setView(this);
    update(model, null);

}


private void createGameWindow() {
    frame = new JFrame("CONNECT FOUR");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    createStatsPanel();
    createGamePanel();
    createButtonPanel();

    frame.getContentPane().add(statsPanel);
    frame.getContentPane().add(gamePanel);
    gamePanel.add(buttonPanel);

    frame.setBounds(100, 100, 750, 450);
    frame.getContentPane().setLayout(null);
    frame.setResizable(false);
    frame.setVisible(true);
}

GUI screenshot for reference: GUI

Any help would be very much appreciated!

P. Tan
  • 43
  • 1
  • 2
  • 8
  • 3
    OK, to help us help you -- simplify this problem. Yes, have 3 classes, but small **compilable** and runnable classes that have fewer components on them, that just try to get the concept of MVC working. We don't want your whole program, we want your *problem* distilled into a smaller program. – Hovercraft Full Of Eels Oct 16 '16 at 15:04
  • 1
    Essentially, I'm requesting that you put int he effort to create and post a valid [mcve] or [sscce](http://sscce.org). Please check the links. – Hovercraft Full Of Eels Oct 16 '16 at 15:04
  • 2
    Also, you really need to learn about and user arrays and ArrayLists as your code has a ton of unnecessary and confusing repetition. Just doing this alone will make your program easier for you and us to understand and debug. – Hovercraft Full Of Eels Oct 16 '16 at 15:06
  • @HovercraftFullOfEels Have updated the post to show just the relevant bits of code. – P. Tan Oct 16 '16 at 15:24
  • 1
    If you still need our help, please read or re-read the links I've posted above, since the code you've posted does not qualify for either an MCVE or an SSCCE -- yes it's short, but we can't compile it, we can't run it, we can't test it, it and it doesn't reproduce your problem for us. Please comment back once you've done this and I'll be glad to take a look. Note that as usual, all code should be posted here as code-formatted text (as you're currently doing) and not in a link. Good luck! – Hovercraft Full Of Eels Oct 16 '16 at 15:27
  • 2
    There is no need for repaint(). When you invoke the setText() method the component will repaint itself. It the text doesn't change then you don't have a reference to the component that is visible on the frame. You should not be invoking the update(...) method directly. This will be done by the class that extends Observable when it invokes the setChanged() and notifyObservers() metods.. – camickr Oct 16 '16 at 17:44

0 Answers0