Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!!!!!!!

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

![]() |
![]() |
![]() |

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