Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning String from ActionPerformed

 
Alex Ter
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the path to the picture an instance variable of some class. And then when you use the FileChooser to select the image- You can get the path and then set the path to the instance variable.


This can be one of the approaches.
 
Maneesh Godbole
Saloon Keeper
Posts: 11085
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Pseudo code
1) Subclass JPanel
2) Have a JLabel instance variable created and added to this panel
3) Provide a method called loadImage(File imageFile)
4) Add this panel to the frame. Initially it will show nothing
5) After user selects the image, invoke method in #3 above
5) Inside the method implementation
a)Store the File as an instance variable for later access
b)Set the image as an icon to the label instance in #2 above
 
Maneesh Godbole
Saloon Keeper
Posts: 11085
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kevin.
I edited your post to correct the cross post link. You had posted the URL for this thread itself

Alex,
Please read http://www.coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites which explains why crossposting without informing is a bad idea.
 
Alex Ter
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help for those who cared and for the virulent cross post hunter I really really apology. 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
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Ter wrote:thanks for the help for those who cared and for the virulent cross post hunter I really really apology. 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


Appreciate the apology, but just for future reference, if you read the link about cross-posting provided to you above you would have understood that the problem is not that many volunteers are on other forums, it's that many are not, who risk wasting time answering a question that's already been answered elsewhere.

Luck
 
Darryl Burke
Bartender
Posts: 5147
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also learn the meaning of the adjective you applied to the 'cross post hunter'
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Also learn the meaning of the adjective you applied to the 'cross post hunter'
At least you apologised, but have a look at this FAQ, Alex Ter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic