简体   繁体   English

来自Observable的update()调用未调用

[英]update() call from Observable not called

I have an Observer which my Window class. 我的Window类中有一个Observer。 I have a Model class that extends Observable containing an ImagePanel class, and a Controller class. 我有一个扩展了Observable的Model类,其中包含一个ImagePanel类和一个Controller类。 I do add to my Model instance the window. 我确实将窗口添加到我的Model实例中。

My problem is : I do a print before model.notifyObservers() , it works, another print after this call, it works too. 我的问题是:我在model.notifyObservers()之前进行打印,它可以工作,在此调用之后进行另一次打印,它也可以工作。 But the print inside the update() of my Observer method doesn't show up? 但是我的Observer方法的update()内部的打印没有显示吗?

From my controller I call this setImage method: 从我的控制器,我调用此setImage方法:

public void setImage(File file)
    {
        try
    {
        image = ImageIO.read(file);
        fileName = file.getPath();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    width = image.getWidth();
    height = image.getHeight();
    imageType = image.getType();
    pixels = new int[width * height];
    image.getRGB(0, 0, width, height, pixels, 0, width);
    this.setLocation(1000, 500);
    System.out.println("ALRIGHT2");
    model.change();
    if (model.hasChanged())
    {
        System.out.println("ALRIGHT5");
        model.notifyObservers();
        System.out.println("ALRIGHT6");
    }
}

This is the update method of my Observer: 这是我的观察者的更新方法:

public void update(Observable o, Object arg) {
    System.out.println("ALRIGHT3");
    image_panel.repaint();
    scrollPaneImage.repaint();
}

And I did this in my Observer constructor: model.addObserver(this); 我在Observer构造函数中做到了这一点: model.addObserver(this);

What's wrong? 怎么了? I assume that the image I chose thanks to a JFileChooser doesn't update because this method is not called... 我认为由于JFileChooser而选择的图像不会更新,因为未调用此方法...

Resolved ! 解决 ! Sorry for the inconvenience, my JScrollPane contains a JLabel which itself contains an ImageIcon and I forgot to update this ImageIcon... 抱歉给您带来不便,我的JScrollPane包含一个JLabel,它本身包含一个ImageIcon,我忘记更新此ImageIcon了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM