Paint foreign component (i.e. belonging to a different frame's content pane) in another component

与世无争的帅哥 提交于 2019-12-04 19:29:44
trashgod

Sorry, I can't address your question directly, but it may be possible to avoid it by having two different views of a common model. As shown in How to Write a Document Listener, it is possible to update more than one view of a Document. Your model and view(s) might be different, but the concept would still apply.

Addendum:

I think that is what I am doing at the moment, isn't it? I have one model i.e. the 'foreign' component, and two views one doing default paint and second custom paint.

No, you have one view updating another; you need two views responding to one model. This related example may offer some insight.

I am still interested in suggestions as to the reasons of this erroneous painting.

The attempt to interleave the updates of two components is, IMO, fundamentally flawed; it subverts the normal process of Painting in Swing. On my platform, I see only a very brief flicker and no adventitious painting. Although it may be possible to obtain satisfactory results on one system, the arrangement would be unreliable across implementations.

Is a simple call to repaint of the label, placed in the ModelObserver's update() method the appropriate solution?

Yes, repaint(), but it should be done in the ButtonHandler for View:

private class ButtonHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        PieceButton pb = (PieceButton) e.getSource();
        icon.color = pb.piece.color;
        label.repaint();
        model.check(pb.piece);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!