Communication between two different JFrames?

限于喜欢 提交于 2019-12-17 16:31:06

问题


Hello as maybe you have heard about GIMP or something like that which uses different frames As a complete gui so I was wondering how to do such frames communications when both(maybe multiple) frames are loaded in memory and are visible.

I have gone through some articles but they were not so satisfactory, if anyone have a good example or tutorial then please share.

Regards Alok sharma


回答1:


Basically, it's just a matter of having a reference to frame A in frame B, and a reference to frame B in frame A :

public class FrameA extends JFrame {
    private FrameB frameB;

    public void setFrameB(FrameB frameB) {
        this.frameB = frameB;
    }

    public void foo() {
        // change things in this frame
        frameB.doSomethingBecauseFrameAHasChanged();
    }
}

public class FrameB extends JFrame {
    private FrameA frameA;

    public void setFrameA(FrameA frameA) {
        this.frameA = frameA;
    }

    public void bar() {
        // change things in this frame
        frameA.doSomethingBecauseFrameBHasChanged();
    }
}

public class Main {
    public static void main(String[] args) {
        FrameA frameA = new FrameA();
        FrameB frameB = new FrameB();
        frameA.setFrameB(frameB);
        frameB.setFrameA(frameA);
        // make both frames visible
    }
}

Most of the time, interfaces are introduced to decouple the frames (listeners, etc.), or a mediator is used in order to avoid too much linkings between all the frames, but you should get the idea.




回答2:


If you are separating out your "Control" logic from your "View" logic in a MVC type pattern this should be as simple as just referencing a different component.

Just like a JFrame might have multiple panels and your application can make changes to several panels based on actions in a single panel. Your application can have multiple frames that can be affected by actions in a single frame.




回答3:


Consider to build your application on top of the NetBeans Platform (a Swing-bsed RCP), which comes with a window system. You can have the TopComponets detached, if you prefer multiple windows.

You can communicate between TopComponents and Modules via a Lookup instance.




回答4:


I'm 7 years late, but maybe this is still interesting for you ;) I solved this issue by implementing the Observer-Pattern.

In my example there is an overview-view (=observer; table from DB) and a detail-view (=observable; contains a row from overview-view). Now I want to edit a (in the overview-view) selected row in the detail-view, click save-button, close the detail-view and notify the changes to (all) observers.

public interface Observer { 
    public void notifyUpdate(Contact contact); 
}

public interface Observable {   
    public void addObserver(Observer observer);
}

public class Detail extends JFrame implements Observable {
    /* multiple observers possible */
    private ArrayList<Observer> observers;
    /* contact will be modified in detail-view and reported to overview-view */
    private Contact contact;
    public Detail() {
        this.observers = new ArrayList<>();
    }
    @Override
    public void addObserver(Observer observer) {
        this.observers.add(observer);
    }
    /* trigger this methode e.g. by save-button, after modifiing contact */ 
    protected void notifyObservers() {
        /* multiple observers possible */
        Iterator<Observer> observerIterator = this.observers.iterator();
        while(observerIterator.hasNext()) {
            /* report observer: constact has modified */
            observerIterator.next().notifyUpdate(contact);
        }       
    }
}

public class Contacts extends JFrame implements Observer {
    private Detail detail;
    public Contacts() {
        detail = new Detail();
        detail.setVisible(true);
        detail.addObserver(this);
    }
    @Override
    public void notifyUpdate(Contact contact) {
        /* notifyUpdate was called from Observable */           
        /* process contact, DB update, update JTable */
    }
}


来源:https://stackoverflow.com/questions/5127140/communication-between-two-different-jframes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!