Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

How to remove JFileChooser  selections?

Thread view: 
Rick - 24 May 2004 18:06 GMT
I have one instance of a JFileChooser. If I select a file using the
JFileChooser then cancel the operation, the same file is shown as
selected the next time the JFileChooser is displayed. This happens
despite having reset the selected file(s) before the second display.
How can I redisplay the chooser without showing the previous
selection?

The code below does nothing but illustrate the situation. Press
"Create Index", select a file from the JFileChooser then press
"Cancel". Press "Create Index" again to see your file selected.
(I'm using java version 1.4.1)

package imageViewer;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;

public class JFileChooserTest extends JFrame {
    private JButton btnCreateIndex;
    private JButton btnCancel;
    private JFileChooser jpegFileChooser;
   
    public JFileChooserTest(String title) {
        super(title);
        init();
    }
   
    public void init() {
        setSize(400, 100);
        getContentPane().add(getBtnCreateIndex(), BorderLayout.WEST);
        getContentPane().add(getBtnCancel(), BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args) {
 
        new JFileChooserTest("JFileChooserTest Clear Selection").show();
    }

    public JButton getBtnCreateIndex() {
        if (btnCreateIndex == null) {
            btnCreateIndex = new JButton("Create Index");
            btnCreateIndex.addActionListener(new AbstractAction(){
                public void actionPerformed(ActionEvent ae){
                    showJpegFileChooser();
                    }
                }
            );
        }
        return btnCreateIndex;
    }
    /**
    * @return Returns the jpegFileChooser property.
    */
    public JFileChooser getJpegFileChooser() {
        if (jpegFileChooser == null) {
            jpegFileChooser = new JFileChooser("");
           
            jpegFileChooser.setAcceptAllFileFilterUsed(false);
            jpegFileChooser.setMultiSelectionEnabled(true);
        }
        return jpegFileChooser;
    }

    /**
    * @return Returns the btnCancel property.
    */
    public JButton getBtnCancel() {
        if (btnCancel == null) {
            btnCancel = new JButton("Cancel");
            btnCancel.addActionListener(new AbstractAction(){
                public void actionPerformed(ActionEvent ae){System.exit(0);}
            });
        }
        return btnCancel;
    }   

    protected void showJpegFileChooser(){
        getJpegFileChooser().setSelectedFile(null); //?
        getJpegFileChooser().setSelectedFiles(null); //?
        int status = getJpegFileChooser().showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            // stuff
           
        }
    }
}

Thanks for any advice.

Rick
Rick - 26 May 2004 12:36 GMT
I figured out a way to reset (clear) the selected files before
redisplaying my JFileChooser.

File file = new File("");
File[] files = {file};

chooser.setSelectedFile(file);     //My mistake was
chooser.setSelectedFiles(files);   //passing null to these methods.

chooser.showOpenDialog(this);

this worked for me. Please advise if there is a more 'correct'
solution.

Rick.
Roedy Green - 26 May 2004 20:09 GMT
>File file = new File("");
>File[] files = {file};
[quoted text clipped - 6 lines]
>this worked for me. Please advise if there is a more 'correct'
>solution.

you might try  File[] noFiles = new File[0];

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.