JFrame and FrameView object interaction?

How is it possible to access a FrameView (Being the main panel view) object from a JFrame and vice versa??!
snajalmAsked:
Who is Participating?
 
for_yanCommented:

Check this simple example how
a window intercats with two othwer windows
It  does not matter if those are separate windows or panel class
witihin window - it is just how classes dela with handles to other clases
and execute their methods

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyMainWindow extends JFrame implements ActionListener {

      JTextField txt;
      JButton button;
      MyChildWindow child1;
      MyChildWindow child2;


    public MyMainWindow(){
        super("main");
     txt = new JTextField(10);
      button = new JButton("press");
        button.addActionListener(this);
            Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(txt);
        c.add(button);
          this.setSize(200,200);
        this.setLocation(100, 100);

      child1 = new MyChildWindow(this, 1);


           child2 = new MyChildWindow(this,2);


        child1.setVisible(true);
        child2.setVisible(true);
        this.setVisible(true);



    }
       public void setText(String s){
        txt.setText(s);
    }


    public void actionPerformed(ActionEvent e) {
            child1.setText(txt.getText());
          child2.setText(txt.getText());


    }

    public MyChildWindow getChild(int i){
        if(i==1)return child1;
        else return child2;
    }


    public static void main(String[] args) {
        new MyMainWindow();
    }


}

class MyChildWindow extends JFrame implements ActionListener {
    MyMainWindow parent;
       JTextField txt;
      JButton button;
    int num;

    public MyChildWindow(MyMainWindow parent, int num){
        super("child " + num );
        this.parent = parent;
        this.num = num;
           txt = new JTextField(10);
      button = new JButton("press");
           button.addActionListener(this);
        Container c = this.getContentPane();
          c.setLayout(new FlowLayout());
        c.add(txt);
        c.add(button);
        this.setSize(200,200);
        this.setLocation(num*200, num*200);

    }

    public void setText(String s){
        txt.setText(s);
    }

       public void actionPerformed(ActionEvent e) {
           parent.setText(txt.getText());
              MyChildWindow childNotMe = null;
           if(num == 1){
              childNotMe = parent.getChild(2);


           } else    childNotMe = parent.getChild(1);
           childNotMe.setText(txt.getText());



    }


}

Open in new window

0
 
for_yanCommented:
Whe you create  FrameView say your JFrame pass the instance of JFrame to FrameView construyctor
and make thi instance a n instance variable to FerameView - to through tgha hanlde you woul be able to access methods
0
 
for_yanCommented:

Somethidn  like that:



public class MyMainApp extends JFrame {

FrameView fv;

public MyMainApp() {

...

fv = new FrameView (this,....);



}

void moethod1() {
fv.setText(String s);

}

}


class FrameView extends JPanel {
MyMainApp mma;

public FrameView( MyMainApp mma, ...) {
this.mma = mma;


...

}


void method1 () {
mma.setColor(Color.blue);


}





}

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
snajalmAuthor Commented:
No no,

There is a classA which is of type FrameView
and Class B which is of type JFrame

now class B wants to access class A and be able to update the objects there!

ClassA contains the main application panel
0
 
for_yanCommented:
That is exactly what I meant - name  MyMainApp asclassA and FrameView as classB
0
 
for_yanCommented:
If you don't have such sitautaion that instance of classB is created in classA - that does not change the situation, as then
theree ios aome classC which creates both of them
The same ideaolgy should work - classC will pass instances of of itself  to A and to B
and they can communicate through these handles which they
pass each other
0
 
for_yanCommented:
If classC creates both of them then it will maintain instance vraibales pointing to instances of classA and classB
and can have method getInstA() { return instA; }
and then classB can call this method obtaine handle on classA and execute methods of classA in this way.
0
 
snajalmAuthor Commented:
Would this work so that when I call the JFram class within the FrameView class, I pass the calling FrameView to the JFrame constructor?!  Of course I'd need to create a constructor that take the FrameView class as its parameter.  Actually I did that and I'm betting access to some public method but not the public variables in there!
0
 
for_yanCommented:
Constructor is one obvious way, but you can also have set- get methods.
Once you maintain instance variable pointing to JFrame in FrameView, you may havd in FrameView setJFrame(JFrame ff) method
and set it  at some other point. Jaust make sure that you do not try to use this handle
before you set it, otherwise you'll have nullpointer execption
0
 
for_yanCommented:
I usually don't access varaibales (even public) directly.
It is better organized if for all those pieces which you want to access
form another class you have a method.
Say, you can say JFrame.myTextField.setText(...) and myTextField is public, but I'd rather
prefer to have methiod in JFrame, say setMyText(...) and then from FrameView you say
handle_to_Jframe.setMyText(...)
That looks cleaner. Most people even recomment to have all these elemnents private
and expose them only throuygh methods
0
 
snajalmAuthor Commented:
Thanks a million!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.