Hi all,
I know this king of issue is quite common but I'm brand new in java so I found clues about my problem but didn't manage to make it works. Basically, I'm trying to build a small image processing program in which you load an image and then do simple stuff like rotating, changing colors, etc....
So far (which is very close for the moment...) I have a main frame which has a menu item saying "load image". When I click on it opens a file chooser, i select a picture. I would like to create a panel and then display it on the main frame.
So to do that, I retrieve the path of the picture from the file chooser in the ActionPerformed method but I wondering what to do then.

The thing is that I have to draw this picture on a panel then add it to the frame but after this I'll have to reuse it all the time when I'll do some modifications in the image. So I guess I shouldn't create the panel in the ActionPerformed method, should I ?
If not how can I return the string path of the picture from the ActionPerformed ? I tried to do it with setter/getter methods but probably didn't do it properly. Could you give me a bit of help regarding the right way to implement this kind of program ?
thanks
Here is my code for the main window, I'm using netBeans 6.9.

The default actionPerformed method doesn't allow you to return anything (the question would be what are you returning the value to? It doesn't make any sense or follow the event-driven model).

Instead, what you should do is create a variable inside your object who's value gets changed by the actionPerformed method.

Something like this:

Code Java:

publicclass MyClass implementsActionListener{privateFile imagePath;publicvoid actionPerformed(ActionEvent e){// Note: this assumes that every time this method is called you want to change the imagePath.JFileChooser chooser =newJFileChooser();if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
imagePath = chooser.getSelectedFile();// a better practice at this point would be to trigger an event that the imagePath has changed so the rest of your GUI can be updated. However, this is a bit more advanced and isn't really necessary}}}

December 17th, 2010, 03:59 PM

Bill'o

Re: returning String from ActionPerformed

thanks for hints... alright Sirs, I really wasn't aware about cross posting issues as I never really got any help from forums and didn't expect that same people were on all the forums as I don't see the point to have then so many different ones.... anyways I'm really sorry

December 17th, 2010, 10:16 PM

helloworld922

Re: returning String from ActionPerformed

This forum doesn't disallow cross-posting, though we would like it if you posted links to the forums you did cross post at. This save us some time if your question has already been answer at another forum.