-3

I have two windows opened at the same time ,so now on clicking a Jbutton in first window frame My Jlabel value should get updated or change in second window frame.... My problem is I am using observer pattern in my case code is working pretty fine I am able to catch latest updated value but while updating the label giving settext its not getting updated

1] This is my function (called when temp value is changed and I click update button in first frame) .. This function I am using in Observable class --

public void updatetemp(int temp) {
    System.out.println("when temp is updated "+temp);
    this.temp = temp;
    setChanged(); // it says that we have changed somethoing so you need to notify observers
    System.out.println("uiyh");
    notifyObservers(temp); //notify
    System.out.println("notified");
}

2] Now it will notify the observer that there is change in temperature here , I am expecting that my curTempLbl.setText("" + curTempupd); in the code should update the changed value in the second frame ... but it is not getting updated ... Note : If you see I have given system.out.println before and after setting the the jlabel I am able to catch the correct updated value ... but itsnot getting updated in Jlabel please help

     public void update(Observable obj, Object arg1) {
    // TODO Auto-generated method stub
     if(arg1 instanceof Integer){

        int  curTempupd = ((Integer)arg1).intValue();

        System.out.println("inside update value --------"+curTempupd);

        WeatherTablePanel frame = new WeatherTablePanel(weatherDb);

        frame.setVisible(true);
        curTempLbl.setText("" + curTempupd);

        //curTempLbl.repaint();  

        System.out.println("after update value --------"+curTempupd);

    }

This is the code for frame Where I want my Jlabel to get uupdated

public WeatherTableFrame(WeatherDb weatherdb) {
  weatherTblUI = new WeatherTableUI(weatherdb);
  WindowListener exitListener = new FrameTerminator();
  addWindowListener(exitListener);
  setTitle("Weather Table");
  setSize(575,265); 
  add(weatherTblUI);
}

  // another class

      public WeatherTableUI(WeatherDb weatherDb) {
      weatherTblPanel = new WeatherTablePanel(weatherDb);
      addNewLocPanel = new AddNewLocPanel(weatherDb, weatherTblPanel);
      setLayout(new BorderLayout());
      add(weatherTblPanel, "Center");
      add(addNewLocPanel, "South");
      removeBtnPanel.add(removeLocBtn);
      add(removeBtnPanel, "East");
      removeLocBtn.addActionListener(this);
      }

3] inside weatherTblPanel I am trying to update the Jlabel value

I am using thread in my update function code but still its not getting updated please help

@Override
public void update(Observable obj, Object arg1) {
    // TODO Auto-generated method stub
    if(arg1 instanceof Integer){

        int  curTempupd = ((Integer)arg1).intValue();

        System.out.println("inside update value --------"+curTempupd);

    //  

        new Thread(){  
              public void run(){  
                try{Thread.sleep(2000);
                }catch(InterruptedException ie){ie.printStackTrace();}  
                SwingUtilities.invokeLater(new Runnable(){  
                  public void run(){  
                      curTempLbl.setText("" + 89); 
                  }  
                });  
              }  
            }.start();  

            //


        //setVisible(true);




                        //curTempLbl.repaint();             




        System.out.println("after update value --------"+curTempupd);

    }   
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • So,... without your code... we're supposed to guess what you may be doing wrong.... how? Seriously though, please give us enough information so that we don't have to make wild guesses, preferably an [MCVE](http://stackoverflow.com/help/mcve). Please check the link for the details of this very useful tool. – Hovercraft Full Of Eels Oct 01 '14 at 21:00
  • 1] This is the function of my Observable class /*12280sv*/ public void updatetemp(int temp) { System.out.println("when temp is updated "+temp); this.temp = temp; setChanged(); // it says that we have chansomethoing //so you need to notify observers System.out.println("before"); notifyObservers(temp); //notify System.out.println("notified"); } /*12280sv*/ – Vaibhavi Shah Oct 01 '14 at 21:08
  • 1) Please don't post code in comments since it loses its formatting making it unreadable. Instead, post any new code to the bottom of your original question by [editing your question](http://stackoverflow.com/review/suggested-edits/5903142). 2) You really should strongly consider creating and posting that [minimal code example program](http://stackoverflow.com/help/mcve) for us to allow us to understand your problem. – Hovercraft Full Of Eels Oct 01 '14 at 21:10
  • Hey, my guess was correct. Please see edit to answer. – Hovercraft Full Of Eels Oct 01 '14 at 22:09

1 Answers1

1

A guess: you're updating the JLabel in the wrong object, i.e., that you've created a new object of the same class, and are updating the JLabel in the non-displayed new object, leaving the old object unchanged.

Note: if you want an answer that's more than just a wild guess, again, really should strongly consider creating and posting that minimal code example program for us to allow us to understand your problem.


Edit
Yes, you in fact are creating a completely new object and changing the JLabel on the new object:

public void update(Observable obj, Object arg1) {
     if(arg1 instanceof Integer){
        int  curTempupd = ((Integer)arg1).intValue();
        WeatherTablePanel frame = new WeatherTablePanel(weatherDb);  // ***** here *****
        frame.setVisible(true);
        curTempLbl.setText("" + curTempupd);
    }   
}

Solution: Don't create a new WeatherTablePanel in your update method. Instead change the state of the already visible WeatherTablePanel instance. How you do this will depend on more code not shown.


Another issue: where do you hook up your Observer with your Observable, where do you call .addObserver(...) on the Observable?

For example,

import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
import javax.swing.event.*;

public class TestObserver {

   private static void createAndShowGui() {
      MyObservable observable = new MyObservable();
      MyObserver observer = new MyObserver();

      observable.addObserver(observer); // ********* Most important line of code!! *********

      JFrame frame = new JFrame("Observable");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(observable.getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Observer", ModalityType.MODELESS);
      dialog.add(observer.getMainPanel());
      dialog.pack();
      dialog.setLocationRelativeTo(frame);
      dialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyObservable extends Observable {
   private int number;
   private JPanel mainPanel = new JPanel();
   private JSpinner numberSpinner = new JSpinner(new SpinnerNumberModel(50, 0,
         100, 1));

   public MyObservable() {
      mainPanel.add(new JLabel("Change Value:"));
      mainPanel.add(numberSpinner);
      mainPanel.setPreferredSize(new Dimension(400, 350));
      numberSpinner.addChangeListener(new NumberChangeListener());
   }

   public int getNumber() {
      return number;
   }

   public void setNumber(int number) {
      this.number = number;
      setChanged();
      notifyObservers(Integer.valueOf(number));
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private class NumberChangeListener implements ChangeListener {

      @Override
      public void stateChanged(ChangeEvent e) {
         setNumber(((Integer) numberSpinner.getValue()).intValue());
      }
   }
}

class MyObserver implements Observer {
   private static final String LABEL_TEXT = "Number is: ";
   private JPanel mainPanel = new JPanel();
   private JLabel label = new JLabel(LABEL_TEXT + 50);

   public MyObserver() {
      mainPanel.add(label);
      mainPanel.setPreferredSize(new Dimension(150, 100));
   }

   @Override
   public void update(Observable o, Object arg) {
      int value = ((Integer) arg).intValue();
      label.setText(LABEL_TEXT + value);
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373