Results 1 to 5 of 5
  1. #1
    Billoto is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    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.

    Java 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
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,964
    Rep Power
    8

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OP, if you feel you must cross-post, that's fine, but if you have the least bit of consideration for us and the volunteers at other forums, please give us links to all of your cross-posts so that we don't waste time answering questions that have already been answered elsewhere. thank you for your cooperation.

  5. #5
    Billoto is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Apology

    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

Similar Threads

  1. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  2. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  3. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 07:49 AM
  4. returning String from actionPerformed
    By hardcore_teddy in forum New To Java
    Replies: 2
    Last Post: 05-17-2008, 05:32 AM
  5. Returning a string (Partly Bold)
    By TampaTechGuy in forum New To Java
    Replies: 3
    Last Post: 02-03-2008, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •