I'm sorry if this has been answered before. I tried searching, but I'm not even sure exactly what I'm looking for.
What I want is this:
I have a class that creates a JFrame holding a menubar and a scrollpane that holds a JTextArea.
From that panel I call another class that loads an xml file and processes it.
All of this works perfectly. The problem is that I want the 2nd class (the Parser) to write information to the JTextArea from the first class, but can't seem to reference it. Although I could pass the data back as an array to the first class and print it from there, I'd like to know how to do this.
The first class has to provide a method which accepts that text. Internally that method would update the JTextArea in whatever way you wanted.
Then the second class would have to have a reference to the instance of the first class, so that it could call that method. So the second class would have to have a method (or constructor) which would accept and save that reference.
Crude version of that:
By the way this is the answer to all questions of the form "How do I get one class to talk to another class".
There are also more complex ways of doing this: for example you might have a third class which controls interactions between all of your various GUI classes. This might even be better design, but given that we know almost nothing about your application, let's just start with the simplest thing first.
Joined: May 30, 2011
I'm very sorry for being dense. I'm confused about this and I might in fact be wrestling with more than one issue at the same time.
In your example, I would need to call SecondClass from FirstClass, correct? Something like
I didn't want to post the code because it is badly done, a stupid project, and I'm frankly a bit embarrassed.
But it flows like this:
Includes just a call to FirstClass
Creates the JFrame with the JTextArea
Calls the menubar class
This creates the bar and menus, including the "ActionPerformed" method
that calls the XMLParser
Opens and reads the file, parsing the XML
And this is where my issue is. I want the XMLParser to write its output to the JTextArea from FirstClass
Do I need to keep passing that reference to it from one class to the next? I think it would be better to call the parser from FirstClass, but I'm not sure how to move the ActionPerformed method out of the menubar and into FirstClass. Should I just put the menubar code in FirstClass rather than in its own class?
Cody Wade wrote:I didn't want to post the code because it is badly done, a stupid project, and I'm frankly a bit embarrassed.
You wouldn't be the first programmer to do that. Hands up anybody who never did that? Anybody? ...... Nobody? ..... Okay then.
And this is where my issue is. I want the XMLParser to write its output to the JTextArea from FirstClass.
But you shouldn't want that. The job of the XMLParser is to parse an XML document and (from what you say) to produce some output. You shouldn't also make it responsible for writing its output to some GUI component; if you do that then you don't have a simple description of what it's responsible for, and that's a symptom of poor object-oriented design. (The description would say "Must parse an XML document into (some text format) and write that text into a text area"; when you see the word "and" in a description you know it isn't right.)
That's really what we are talking about here, object-oriented design. You haven't done that yet (not surprisingly for a beginner) and so now you're stuck in the mud. You're still talking about classes calling classes (again, not surprisingly for a beginner) and that isn't all that helpful once you get more than one class.
So let me see if I can say something useful about the classes you have there. You've got a Menubar object. You say it creates a menu bar, and then it's also calling an XML parser when some menu option is selected. There's that word "and" again... but really the creation of the GUI components is incidental. Let's just say it has a menu bar; it might create that itself in its constructor, or it might get it in some other way. Its main purpose is to act on menu-bar clicks.
And when one particular menu item is clicked, it's supposed to act on that by calling an XML parser and dealing with the output of that parser. Specifically it's supposed to deal with that output by writing it to some other GUI component. But that GUI component isn't part of the Menubar class, so it's the responsibility of some other object. Therefore to deal with the output, the Menubar object has to contact that other object and say "Hey! Here's the text you need!". It isn't up to the Menubar object to know that it's going to be put into a GUI component, that's not Menubar's business. It's up to that other object to know what to do with the text. Let's call it a TextHandler for now. Since it's nobody else's business what a TextHandler is going to do with that text, TextHandler just needs a method which accepts a String.
Now notice I've been talking about objects and not about classes. All of the work in your system is going to be done by objects and not by classes. So you'll need a Menubar object to do Menubar stuff, and a TextHandler object to do TextHandler stuff. And since the Menubar object needs to pass information to the TextHandler object, it needs to keep a reference to the TextHandler. That way it can call the TextHandler's method which accepts the String.
So: Object-oriented design includes a division of responsibilities. Each object has a single responsibility and each system requirement is handled by only one object. As far as possible, that is. That's what I have been describing here and it should coincide with the crude example I posted earlier. At least I hope it does.
Joined: May 30, 2011
Thank you very much, Paul. I'm sure you get tired of posting basic OOP information, but as a beginner, let me say "Thank you! Thank you! Thank you!".
I think you are very correct--I am sort of mimicking OO design without fully understanding it (Probably also a symptom of a beginner ;) ). It's going to take me some time to process that information, and to rethink what I'm doing. I am not sure how to accomplish what needs to be done, but at least now I have an idea where to look.