?
Solved

Java Frames

Posted on 2003-03-24
10
Medium Priority
?
182 Views
Last Modified: 2010-04-17
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








0
Comment
Question by:mythreyiv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 3

Expert Comment

by:NetWize
ID: 8200573
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?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8200601
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.
0
 

Author Comment

by:mythreyiv
ID: 8203000
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
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:mythreyiv
ID: 8203030
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8203173
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8203187
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.
0
 

Author Comment

by:mythreyiv
ID: 8203276
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
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8203387
Put both the classes in the same program (or let them be as they are), and let the second class instantiate an object of the first class (hence it will also be displayed that way) - and both your purposes will be achieved. Otherwise, you cannot let the second program read the data from the first one that way - they won't communicate this way! You either need to send packets between them through networking classes or you need to interact through files. Remember, if you're running the first program just like that, then it is independent of the second program, and if the second program also runs simultaneously, it has nothing to do with the currently executing process (the first program).

I still say that you should let only the second program run and also display the JFrame by instantiating an object, and then proceed.

I'm leaving for the day right now.... see you tomorrow. Please keep this page updated.

Mayank.
0
 

Author Comment

by:mythreyiv
ID: 8203436
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8207801
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.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans
Suggested Courses

752 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