DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   calling another function in nested class (http://www.daniweb.com/forums/thread87678.html)

w32.sysfile Aug 25th, 2007 6:10 am
calling another function in nested class
 
this is my code..
class NewAction implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
 
                TArea2.setDocument(new PlainDocument());
                }
        }
        void confirmation()
        {
        int ReturnValue=JOptionPane.showConfirmDialog(null,"Do you want to open a new file?    ","Confirmation  ", JOptionPane.YES_NO_CANCEL_OPTION  );
 
        if(ReturnValue==JOptionPane.YES_OPTION)
        {
                //HOW CAN I POSSIBLY CALL THE METHOD IN THE NewAction class?? 
        }
        else if(ReturnValue==JOptionPane.CANCEL_OPTION)
        {       
        }
        }


sorry for my newbie question but can we actually call the actionPerformed method inside the NewAction class from the confirmation method??

i did try some like
this.NewAction().actionPerformed();
myclass.NewAction.actionPeformed();
NewAction().actionPerformed();
NewAction.actionPerformed();

i dont know any possibilities anymore..
help me please..
i changed the NewAction class become public but still doesnt work..


thanks

jwenting Aug 25th, 2007 9:35 am
Re: calling another function in nested class
 
Think about what you're doing:
You want to call a method on a class, so you'd better make sure you have an instance of that class.

In this case that class is an inner class to another class, so you must first have an instance of that other class.

That leads you to something like
this.new NewAction().actionPerformed();

w32.sysfile Aug 25th, 2007 2:07 pm
Re: calling another function in nested class
 
it doesnt work, but thanks

i think it is better to changet the hierarchy, i realize that my code is confusing

jwenting Aug 25th, 2007 2:40 pm
Re: calling another function in nested class
 
well, based on your bit of code something like that should work.
Problem is that that code is rather ambiguous and it is of course not at all clear what you're actually trying to accomplish.

w32.sysfile Aug 25th, 2007 8:29 pm
Re: calling another function in nested class
 
ok then, actually i am making a notepad like program, i want whenever i press the "New" , "Open", Exit" buttons, it will prompt me for saving, but now i am confused with this..

Ezzaral Aug 27th, 2007 1:32 pm
Re: calling another function in nested class
 
You could of course just put that code in a regular method and call it from your other listeners. Why do you feel it needs to be another class?

If it does need to be a class, then yes, you can call it like jwenting wrote, but of course you will need to supply the method parameter. A null will suffice if you aren't actually using the ActionEvent param for anything. If you tried to call it with no parameter then certainly it didn't work.

orko Aug 28th, 2007 1:26 pm
Re: calling another function in nested class
 
i think u shud try the following:
(new NewAction()).actionPerformed();
just one extra () :)


All times are GMT -4. The time now is 10:17 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC