0

I have Observable class which notifies observer when string changes. In the observer's update method the updated string can be printed to console. But GUI is not updated accordingly. Why?

public void update(Observable o, Object arg) {

    String s=SwimmingCompetition.init().getScoreData().getResults();
    System.out.println(s); //this works
    jTextArea1.setText(s); //this not

}
chamathabeysinghe
  • 858
  • 2
  • 13
  • 34
  • 1
    There's no way for us to tell what's wrong based on what you've posted. Guesses include -- you may be tying up the Swing event dispatch thread, and if you were doing this, the GUI would be completely unresponsive to user interaction, you might be shadowing the GUI by creating more than one instance, and changing the non-visualized instance,... You're going to need to post more pertinent code.... how much? hard to say, but not too much and not too little! ;) Best would be an [mcve]. – Hovercraft Full Of Eels Nov 01 '15 at 14:48
  • Not related to your problem, but I'm curious as to why you don't use the `arg` argument within your `update(...)` method? Also why are you calling a static method of SwingCompetition, much less an `init()` type method? It gives me concerns about your overall program design. – Hovercraft Full Of Eels Nov 01 '15 at 14:59
  • 1
    Please comment back to me with an `@Hovercraft` in the comment after you've updated/improved your question. Thanks. – Hovercraft Full Of Eels Nov 01 '15 at 15:15

3 Answers3

3

I bet you are neglecting to invoke addObserver() on your Observer, or neglecting to invoke both setChanged() and notifyObservers() in your Observable. A complete examples is shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

I bet you are manipulating GUI stuff outside the GUI thread. Try this:

public void update(Observable o, Object arg)
{
    String s = SwimmingCompetition.init().getScoreData().getResults();
    System.out.println(s);
    SwingUtilities.invokeLater(() ->
    {
        jTextArea1.setText(s);
    });
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • While yours is a good recommendation, I'm betting that this is not the cause of the problem. – Hovercraft Full Of Eels Nov 01 '15 at 14:52
  • @HovercraftFullOfEels: Because `Observable` may "deliver notifications on separate threads," I'll say this is useful. – trashgod Nov 01 '15 at 16:26
  • @trashgod: I agree, and have not down-voted this. But I'm betting that it still won't explain the cause of the problem, since calling off thread will usually work intermittently, and when it fails, will usually throw some exception. The original poster's question however remains woefully incomplete, forcing us all to guess. – Hovercraft Full Of Eels Nov 01 '15 at 16:32
2

Please understand our predicament. You've posted code and have stated that it's not working correctly, but despite our requests for clarification, as of now, 10 hours later, no additional code or clarification has been forth-coming. And so we're stuck.

What we know is this:

  • You have a method, update(...) that is supposed to be called when an Observable's state has changed.
  • That this method is supposed to change the state of your GUI, that is, it is supposed to change the text shown in a JTextArea,
  • But that it is not doing this.

Without additional information about all we can do is guess. Possible causes include:

  • You're calling long-running code on the Swing event thread, freezing your GUI, so that even though the code that you show is running correctly, the Swing event thread is so busy that it's not able to paint the GUI and update the text area's text.
  • ...or this update method is for some reason not being called due to an error in your (unshown) Observable's code.
  • ...or you have more than one Observable, and the one being changed is not the one that is being observed
  • ...or you hve more than one Observer object, and the one being notified is not the same one that holds the displayed JTextArea.
  • Or the String s does not hold the information that you think it should hold. Again as per my comments, you are not using your update's arguments, and that risks not getting the correct information where you need it.

Again, please fix your question so that we can answer it without having to make wild, and likely incorrect guesses.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    Having encountered a significant fraction of such problems, I heartily endorse an [mcve] as a means to sort thing out. – trashgod Nov 02 '15 at 01:14