JFileChooser

I've got a bit of a problem with JFileChooser. I have a class extending SimpleGame with a JMEDesktop which has some buttons in a menu. When I click on a button, I would like a JFileChooser to appear, let me select a 3DS and then import in the scene.


private void newActionPerformed(java.awt.event.ActionEvent evt) {
        System.out.println("New pressed");
        //newProjectDialog = new JInternalFrame("New Project");
        JInternalFrame tempFrame = new JInternalFrame("New Project");
        //System.out.println("newProjectDialog components count : " + newProjectDialog.getContentPane().getComponentCount());
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.addChoosableFileFilter(new ImageFilter());
        fileChooser.setAcceptAllFileFilterUsed(false);
        //Add custom icons for file types.
        fileChooser.setFileView(new ImageFileView());
        //Add the preview pane.
        fileChooser.setAccessory(new ImagePreview(fileChooser));
       
        tempFrame.add(fileChooser);
        //Show it.
        int returnVal = fileChooser.showDialog(null,
                                      "New Project");

        //Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            System.out.println("Attaching file: " + file.getName());
        } else {
            System.out.println("Attachment cancelled by user.");
        }
       
        //Reset the file chooser for the next time it's shown.
        fileChooser.setSelectedFile(null);
        System.out.println("it should show");
        //this.
    }



The fileChooser does appear, but if I move it a bit around, it looks like this :


So far it crashes my application, if i press alt+tab.

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d15d2e8, pid=1908, tid=240
#
# Java VM: Java HotSpot(TM) Client VM (11.0-b09 mixed mode, sharing windows-x86)
# Problematic frame:
# C  [awt.dll+0xad2e8]
#
# An error report file with more information is saved as:
# E:programareJavaMy projectsTest3hs_err_pid1908.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

If you want to import 3ds files you must not use "Just Images" as file type in file chooser. ~^



You must not use ImageFilter.



Here

fileChooser.addChoosableFileFilter(new ImageFilter());


you have to provide a filter for 3ds files.
madlion said:

If you want to import 3ds files you must not use "Just Images" as file type in file chooser. ~^

You must not use ImageFilter.

Here

fileChooser.addChoosableFileFilter(new ImageFilter());


you have to provide a filter for 3ds files.


I forgot to change the name  :D. But the fact remains that it still crashes and it still messes up the main window (the white part).

Hehe, ok.





About the crash i do not know.



I'm not sure but wouldn't this


//Show it.
        int returnVal = fileChooser.showDialog(null,
                                      "New Project");


will block the application till fileChooser returns!? Perhaps this is why no rendering will be done anymore.

I think so, it looks like it's modal. I can't select anything in the "back"(menus, etc).

Are you doing all of this in the AWT event thread?



If not, take a look at SwingUtilities.invokeLater(), and SwingUtilities.invokeAndWait()


  Finally understoud what darkfrog said in this thread http://www.jmonkeyengine.com/jmeforum/index.php?topic=2676.msg20288#msg20288.

  I’ve declared a JInternalFrame and I put the fileChooser in it in a function which I call in the swing thread. It shows now properly, but now I need to figure out how to make it close or import my object in my scene.


private void createNewProjectDialog() {
        // <editor-fold defaultstate="collapsed" desc="New Project dialog">
        // MOVE dialog


        final JDesktopPane desktopPane = jmeDesktop.getJDesktop();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File("houses"));
        fileChooser.addChoosableFileFilter(new HouseFilter());
        fileChooser.setAcceptAllFileFilterUsed(false);
        //Add custom icons for file types.
        fileChooser.setFileView(new HouseFileView());
        //Add the preview pane.
        fileChooser.setAccessory(new HousePreview(fileChooser));
        newProjectDialog.getContentPane().add(fileChooser);
        newProjectDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        newProjectDialog.pack();
        desktopPane.add(newProjectDialog);
        //</editor-fold>
    }



When I click on the button, I just simply do this :

private void newActionPerformed(java.awt.event.ActionEvent evt) {
        System.out.println("New pressed");
        newProjectDialog.setVisible(true);
        System.out.println("it should show");
      
    }



Still no luck making the JFileChooser close or open the file. Anybody any ideas?

I had to go to this length to make it work:


fileChooser.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                URL url = null;
                if(event.getSource().toString().contains("APPROVE_OPTION")){
                //    System.out.println("Open");
                    try{
                 //   System.out.println(fileChooser.getSelectedFile().toURL());
                    url = fileChooser.getSelectedFile().toURL();
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                }else{
                //    System.out.println("Cancel");
                }
               
                closeFileChooserDialog();
                fireFileChooserClosedEvent(url);
               
   
            }
        }) ;



Thanks a million. It works beautifully, just like you said. :smiley: