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);
}