Link to home
Start Free TrialLog in
Avatar of mythreyiv
mythreyiv

asked on

Java Frames

hi,

I am setting one private varaible in actionperformed method()
(i mean setting that varaible at run time )
in that class i have one method to print that variable which setted in actionperformed().
when i am using this print method in another class, it diplaying nullvalue.

can any body help me in this.

class search()
{

 private String check=new String();
 
 search(String str)
 {

 }
 
 search()
  {
    //create one button
     JButton click=new JButton();
     click.addActionListener(this);
  }

 public actionPerformed()
 {
  if(click.getSource("click"))
   {
       check="yes i got it";
   }
  System.out.println("check="+check);

 }

public void printMethod()
{
  System.out.pritnln("check="+check);
}
 
} //end of first class


class second()
{

 second()
 {
  search src=new search("hello");
  search.printMethod();
 
 }


} // end of second class


1. if run the first program the check valueis set to " yes i got it".
2. if i run the secong program the check value is null

why it is happening.
if i want to get the check value  through second class , what i ahve to do??


thank you
Mythreyi








Avatar of NetWize
NetWize

You create a NEW search in the second prog. Maybe you should pass a reference to the original search to that second class (via constructor?)

Or does the constructor of search() displays a dialog with a button to click?

maybe it's just that the second prog isn't "waiting" for the button to be clicked so it prints out the line before you had a chance of clicking the button?
Avatar of Mayank S
First of all, if you have to access the printMethod () method, then use the object src as:

src.printMethod () ;

otherwise, if you want to continue with your way of accessing it, then declare it to be static in the class search.

Second, how are you executing the program? If you set the value in one program execution, then it will be lost in the second program execution, so how will you be able to get it?

Also, in you constructor search ( String str ), you are not creating the button and declaring the actionListener. But this is the constructor which you are calling from your second class.

And I still wonder how you got that code to compile because:

>> class search ()
>> class second ()

should be:

class search
..
class second
..

Mayank.
Avatar of mythreyiv

ASKER

hey sorry ,

i am using like src.printmethod()only.
that was mistake when i am posting the question


my intention is first
i will execute the program
with that i clck button and set the value.
immediately i exceute the second program.
to print result.
more ever these 2 classes are in a package.

i need more comments.

thankyou
Mythreyi
Hi

the search() method displayes the button to click,
i don't want that in the second class.so i am using another constructor search(Str str) to instatinte the first class object in second class, to access the printmethod.


how can i pass the reference, via constructor.

i need more comments.

thank you
Mythreyi
Don't expect the two programs to communicate with each other simply if they are executing together. I would suggest that you simply run the second program and replace:

>> search src=new search("hello");

with:

search src = new search () ;

This will call the search () constructor where you have added the action-listener to the button but you should move the declaration of the button ( 'JButton click = ..' ) part outside the method so that click is defined as a class-member and accessible in the actionPerformed () method:

public void actionPerfomed ( ActionEvent ae )
{
  if ( ae.getSource () == click )
    check = "Yes, I got it! " ; // end if

} // end of actionPerformed ()


This should be the definition of your actionPerformed () method.

Mayank.
I am assuming that your search class extends JFrame, implements ActionListener and that you have put the setSize () or resize () and setVisible () or show () methods in the constructor.

Mayank.
Hi mayank,

i am doing all of htem.
but i shouldn't display the button when i am executing the second program.
that is my requirement.


so using the first class ,i should set the value
using the second class i should get that value.


Mythreyi
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi mayank,

my requirement won't allow me to run the programs as u said.But now i got the problem what you said.

i think the alternative of writing into files is best idea.
i will use that idea.

 i  will write that value into the file.
i will read that value from second program.


thanks mayank
Myhreyi
Go ahead.... good luck!

[In that case, you don't need to instantiate an object of the first class in the second class at all. You can just run the first program separately, write to the file, then run the second program and let it read from the file.]

Mayank.