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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
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
Mick BarryJava DeveloperCommented:
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
Mick BarryJava DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.