Computing.Net > Forums > Programming > Performing Action on Java Parent

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Performing Action on Java Parent

Reply to Message Icon

Name: drsmartman
Date: September 30, 2004 at 17:07:28 Pacific
OS: Mac OS X
CPU/Ram: NA
Comment:

Hi all,

Just started to get into Java Swing for a GUI application I'm
creating. I have a quick question that seemed simple, but I
can't find an answer....

I have a class that holds the main GUI shell....called
OSPMainFrame (as in "Main" frame...not mainframe).
Anywhoo.....OSPMainFrame (extends JFrame) has a
number of Panels....one of which is populated by an
instance of a class OSPToolBar (extends JToolBar). I
thought it best to breakout the classes this way...to keep
things modular. OSPToolBar has a number of
ActionListeners (they all work) for New File, Open File...etc
which are attached to buttons on JToolBar.

Okay, my question is this. When I run the program
through the main function on OSPMainFrame I do/the
follwing happens....

1.) My GUI interface pops up and is populated as expected
(OSPToolBar and other GUI isntances OSPMenu,
OSPNavigation,etc).
2.) I click on the 'Open' Button on my OSPToolBar....a
JFileDialog comes up as it should.
3.) I choose a file and write to the console that it was
succesful.

What I want to do with a 4th step is have it so the
ActionListener in OSPToolBar can manipulate something in
OSPMainFrame. How do I do this? I tried getParent(),
getClass(), getName()....and I was able to to get the
instance name for the OSPMainFrame constructor and
everything. However, I can't figure out how to run a
method....I tried getParent().setInternalFrame(), but it's an
unrecognized method. I realize this is difficult to
understand...as it is difficult to explain, but if you can
help....much appreciated.

Bottom line...how to call a method in an object A from a
method in Object B that was called FROM Object A. Get
the drit??? Psuedo code below...

---Start portion of OSPMainFrame.java
public static void main(String args[]){

OSPMainFrame ospmf = new OSPMainFrame();
OSPToolBar osptb = new OSPToolBar();

//add containers, panels etc...
//adding toolbar to toolbar panel
ospToolBar.add(osptb);

}
---END

--Start Portion of OSPToolbar.java

openButton.addActionListener(new ActionListener() {

/**
* actionPerformed
*
* @param actionEvent ActionEvent
*/
public void actionPerformed(ActionEvent actionEvent) {

JFileChooser openFc = new JFileChooser();

int returnVal = openFc.showDialog(null,"Open an
Open Scribe Project...");
if (returnVal == JFileChooser.APPROVE_OPTION) {
**********HOW Do I access OSPMainFrame methods from
here after THIS method is called????????????

****psuedo

getInstance of OSPmainFrame where this was called from
perform method on OSPMainFrame - setinternalFrame
}

}
});

---End

Thanks again...I know this is confusing....much
appreciated!!!!!!!



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: September 30, 2004 at 22:01:46 Pacific
Reply:

I haven't used Swing, and never needed to do this in any of my Java projects in school...But in Visual Basic.NET you could reference the object A by saying ObjectB.Container. Also, you can pass object B a reference to Object A so it has access to its methods, or you could have object B raise an event that object A handles.

Hopefully the same concepts apply in Java.

-SN


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Performing Action on Java Parent

batch error numbering files www.computing.net/answers/programming/batch-error-numbering-files/15049.html

help on java programming www.computing.net/answers/programming/help-on-java-programming/3347.html

Get Pi with Java www.computing.net/answers/programming/get-pi-with-java/7688.html