Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: returning String from ActionPerformed

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    /*
     * 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
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: returning String from ActionPerformed


  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:
    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
            }
        }
    }

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

Similar Threads

  1. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  2. actionPerformed method
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 4th, 2010, 02:29 AM
  3. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM
  4. Returning reversed string? Help please.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 18th, 2010, 04:23 PM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM