Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JFrame and FrameView object interaction?

Posted on 2011-10-17
11
Medium Priority
?
602 Views
Last Modified: 2012-05-12
How is it possible to access a FrameView (Being the main panel view) object from a JFrame and vice versa??!
0
Comment
Question by:snajalm
  • 8
  • 3
11 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36983921
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36983931

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
 

Author Comment

by:snajalm
ID: 36983978
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 47

Expert Comment

by:for_yan
ID: 36983983
That is exactly what I meant - name  MyMainApp asclassA and FrameView as classB
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984000
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36984026
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36984041

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
 

Author Comment

by:snajalm
ID: 36984057
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36984065
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36984070
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
 

Author Closing Comment

by:snajalm
ID: 36984103
Thanks a million!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question