FileChooser -> Open button clicked

How do I tell that the Open button has been clicked? My code right loads a file, whether or not the Open button is clicked.

Thanks,
Ryan
jFileChooser_Evaluation.setFileFilter(new XMLfilter());
        jFileChooser_Evaluation.showOpenDialog(mainPanel);
        File newFile = jFileChooser_Evaluation.getSelectedFile();
        if(!newFile.exists()){
            return;
        }

Open in new window

rossryanAsked:
Who is Participating?
 
objectsCommented:
I was referring to if the user for example presses cancel.in that case the if condition will fail and newFile will not be set
0
 
objectsCommented:
check the return value

if (JFileChooser.APPROVE_OPTION==jFileChooser_Evaluation.showOpenDialog(mainPanel)) {
        File newFile = jFileChooser_Evaluation.getSelectedFile();
        if(!newFile.exists()){
            return;
        }
}
0
 
rossryanAuthor Commented:
Hmm. it's throwing an error.


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at bakaranger.BakaRangerView.jButton_OpenMouseClicked(BakaRangerView.java:376)
        at bakaranger.BakaRangerView.access$900(BakaRangerView.java:30)
        at bakaranger.BakaRangerView$5.mouseClicked(BakaRangerView.java:147)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
        at java.awt.Component.processMouseEvent(Component.java:6266)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



Any idea how to fix it?

Thanks,
Ryan
private void jButton_OpenMouseClicked(java.awt.event.MouseEvent evt) {                                          
        jFileChooser_Evaluation.setFileFilter(new XMLfilter());


        File newFile = null;

        if (JFileChooser.APPROVE_OPTION==jFileChooser_Evaluation.showOpenDialog(mainPanel)) {
        newFile = jFileChooser_Evaluation.getSelectedFile();
        if(!newFile.exists()){
            return;
        }
        }


        
        String Name = newFile.getName();

        if(FilenameExists(Name)){
          javax.swing.JOptionPane.showMessageDialog(null,"I'm sorry, but BakaRanger only supports opening one copy of a given filename, at a given time.\nIf you absolutely need to open this file at this time, try renaming the file to something else using Explorer.");
          return;
        }

Open in new window

0
 
objectsCommented:
you need to check the name inside the if (as newFile may not have been set)
0
 
rossryanAuthor Commented:
.....Say that another way?

Shouldn't the function just return if the file doesn't exist? So it would never get to the getName() part...?


-Ryan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.