Hi All,
I am having a bit of an issue moving back into Java.
The issue that I am having is that I am trying to call a method of a custom class that has been instantiated in my main java file from a form that is also in an external file.
The purpose for this is so that I can have an event triggered on the form when a button is pressed to run my method from my default class.
Am I out to lunch?
Should I just instantiate my class in the form class?
Thanks,
corliss

Recommended Answers

All 14 Replies

Or you could just pass a reference of the Object to the Form class and store it there and use it when required...

Pls tell us what is ur exact mean?

Fotsung,

I'm assuming that you mean main class by "form" class.

When you are anywhere in the main class, like the event method, you construct another class in your package like so:

OtherClass anyName = new OtherClass(); 
//OtherClass being the name of your other class

Then, when you want to use a method from that other class you simply write:

anyName.OtherMethod(); 
//OtherMethod being the name of the other method in the other class!
//include any arguments required for method between the paramaters

When you are returning a result from the other method, the call would be:

int result = anyName.otherClass();

I hope this helps.

Regards,
Cleo

Hi All,
Thanks for the reply's.
Unfortunately I don't have a fix yet.
Here is what I mean:
Main.java has

public static void main(String[] args) {
        // TODO code application logic here
        Watcher Beholder = new Watcher();
        NewJFrame GUI = new NewJFrame();
}

There is also Watcher.java that has the watcher class defined and a NewFrame.java file that has the form defined...along with the forms events and swing objects.

So What i am trying to do after instantiating the wathcer class and NewJFrame class in the main.java is to call the instance of watcher(Beholder) in the NewFrame.java file.
Like this:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
Beholder.method();
    }

This is a standard problem with a standard solution:
Change the NewJFrame constructor to take an instance of Watcher as a parameter and store it. So your main now looks like

Watcher beholder = new Watcher();
NewJFrame GUI = new NewJFrame(beholder); // pass Watcher instance to GUI

now in NewJFrame you can use the beholder you have stored to access the variables and methods of your Watcher instance

Thanks James

Yes, and also remember to have your main class to ' extends JFrame ' that will take the above effects James advised.

Good luck! :)

This is a standard problem with a standard solution:
Change the NewJFrame constructor to take an instance of Watcher as a parameter and store it. So your main now looks like

Watcher beholder = new Watcher();
NewJFrame GUI = new NewJFrame(beholder); // pass Watcher instance to GUI

now in NewJFrame you can use the beholder you have stored to access the variables and methods of your Watcher instance

Well I think I should learn to present my solutions as precisely and clearly as James does!
This is the best way to get round this problem without breaking OOP rules.

Yes, and also remember to have your main class to ' extends JFrame ' that will take the above effects James advised.

I am not quite sure as to what this will accomplish as my main.java does not have anything to do with my NewJFrame.java class apart from instantiation.
Can you elaborate as to why I would need to have my main class configured in such a way as to extend the JFrame.

Yes, and also remember to have your main class to ' extends JFrame ' that will take the above effects James advised.

Good luck! :)

Why wud u wanna extend JFrame????

James just asked him to pass the reference of the other object into the GUI class which I think (cant say without seeing code) would be extending JFrame Class to provide swing frame service.

Yes, and also remember to have your main class to ' extends JFrame ' that will take the above effects James advised.

I personally don't understand this post, so it doesn't represent my opinion or advice.
James

I initially said that I was assuming 'form' was your main class, from what I got of 'initiate my class in the form class'. So, I provided advice on how to construct another class, call a method from another class and return the result from another class, to give you the basics. Then when you later provided some idea that you wanted a JFrame I provided advice on the class having to extend the JFrame, as standard practice.

Any thanks for the advice I have tried to give based on your attempted explaination, instead of any kind of discrimination, would be nice.

Hi Cleo
Please don't take any of this personally. People in DaniWeb tend to say exactly what they think, but if they are critical then they are being critical of the post, not the person posting it. (Personally I think you were lucky not to get more flack for your first post here, where you suggested creating a new instance of the target class rather than getting a reference to the existing one.)
And it's a sad fact the vast majority of people who post questions here never bother to say "thank you"; in fact most of them don't even mark the thread "solved", they just get on with their next homework. This really isn't a place for sensitive souls!
J.

J,

I'm not taking this personally. I'm happy for people to say what people think, this is supposed to be a learning-teaching environment after all, and I am highly critical of the fact that people simply want to be critical without providing constructive critism! If you want to critise my code, say what is wrong with it. I provided exactly the same reference to another class that you did, as below, before we finally got to find out that a jframe was required.

OtherClass anyName = new OtherClass();
Watcher beholder = new Watcher();

This thread is solved now, finished, it doesn't matter now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.