lundi 8 juin 2020

nested if statement inside another nested if statement not catch

My java code below features 3 labels called button1, button2, and button3. When the user imports a image it should first go to button1 if and only if there is not a image already on button1. Then the next time a image is import it should go to button to if and only if there is a image on button1. The third time a image should be placed on button3 if and only if button1 and button have images on them. The code is working for button1 and button2 but not for button3. So there is a problem within the nested if statement starting within else{.

  importBtn.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {


           JFileChooser file = new JFileChooser();
           file.setCurrentDirectory(new File(System.getProperty("user.home")));
           //filter the files
           FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
           file.addChoosableFileFilter(filter);
           int result = file.showSaveDialog(null);


           if(result == JFileChooser.APPROVE_OPTION){

               //NotWorking make check null not text
               if  (button1.getIcon() == null) {
                   File selectedFile = file.getSelectedFile();
                   String path = selectedFile.getAbsolutePath();
                   button1.setIcon(ResizeImage(path));   
               }
               else {
                    if  ((button1.getIcon() != null) && (button3.getIcon()) == null){
                        File selectedFile = file.getSelectedFile();
                        String path = selectedFile.getAbsolutePath();
                        button2.setIcon(ResizeImage(path));   
                        }



                        if  ((button1.getIcon() != null) && (button2.getIcon()) != null){
                            File selectedFile = file.getSelectedFile();
                            String path = selectedFile.getAbsolutePath();
                            button3.setIcon(ResizeImage(path));  



               }
               }



           }


           else if(result == JFileChooser.CANCEL_OPTION){
               System.out.println("No File Select");
           }
         }
     });

Aucun commentaire:

Enregistrer un commentaire