returning String from ActionPerformed
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.
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Main.java
*
* Created on 17 déc. 2010, 11:02:40
*/
package JImage;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
/**
*
* @author Bill'o
*/
public class Main extends javax.swing.JFrame {
private String rep = null;
/** Creates new form Main */
public Main() {
initComponents();
}
public void setRep(String s) {
rep = s;
}
public String getRep() {
return rep;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
chargerImage = new javax.swing.JMenuItem();
fermer = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jFileChooser1.setDialogTitle("Charger une image");
jFileChooser1.setFileFilter(new MyCustomFilter());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("JImage"); // NOI18N
jMenu1.setText("Fichier");
chargerImage.setText("Charger une image");
chargerImage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chargerImageActionPerformed(evt);
}
});
jMenu1.add(chargerImage);
fermer.setText("Fermer");
fermer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fermerActionPerformed(evt);
}
});
jMenu1.add(fermer);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edition");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 722, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 469, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public JMenuItem getChargerImage() {
return chargerImage;
}
private void chargerImageActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = jFileChooser1.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String repert = jFileChooser1.getSelectedFile().getAbsolutePath();
setRep(repert);
//JImagePanel panel3 = ImageUtil.makePanel(rep);
} else {
System.out.println("Annulation par l'utilisateur");
}
String imageRep = getRep();
}
private void fermerActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem chargerImage;
private javax.swing.JMenuItem fermer;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration
}
Re: returning String from ActionPerformed
Crossposting isn't against the rules here, but doing it without mentioning it will prevent some people from wanting to help you.
Crossposted here: returning String from ActionPerformed - Java Forums
Re: returning String from ActionPerformed
Re: returning String from ActionPerformed
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:
public class MyClass implements ActionListener
{
private File imagePath;
public void actionPerformed(ActionEvent e)
{
// Note: this assumes that every time this method is called you want to change the imagePath.
JFileChooser chooser = new JFileChooser();
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
}
}
}
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
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.