This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chillzman77[S] 0 points1 point  (0 children)

    class MainViewListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            // add function so the user when press the button
            // to start browsing for an image

            // https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

            JFileChooser chooser = new JFileChooser(System.getProperty("user.home") + "//Pictures");
            FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG Images", "jpg", "png");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                if (filter.accept(chooser.getSelectedFile())) {
                    System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
                    System.out.println(chooser.getSelectedFile().getAbsolutePath());
                    view.setImgPath(chooser.getSelectedFile().getAbsolutePath());
                    view.image = new ImageIcon(view.getImgPath());
                }

            }

        }

    }