How to use Java JFileChooser

How to use Java JFileChooser,

Java JFileChooser Example
Java JFileChooser class is used to create a window that allows a user to select a file.JFileChooser inherits methods from JComponent class.

Declaring  Java JFileChooser 

[class JFileChooser extends JComponent implements Accessible
 
 
]

Java JFileChooser Constructors

  • JFileChooser()-creates a JFileChooser pointing to default directory.
  • JFileChooser(File directory)-create JFileChooser with a given File as  path
  • JFileChooser(String directory)-creates JFileChooser using a certain path.

Java JFileChooser Example

[
/** * * @author Eric 
* Blog:techoverload.net */

import javax.swing.UIManager;
import javax.swing.JFrame;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;


public class JavaJFileChooserExample extends JFrame  implements ActionListener{
    public JMenu file;
    public JMenuBar bar;
    public JMenuItem open,copy,cut,paste,select;
    public JTextArea area;

    public JFileChooser chooser;
    JavaJFileChooserExample()
    {
        super("Java JFileChooser Example");
        try        {
            UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        bar=new JMenuBar();
        file=new JMenu("File");
        open=new JMenuItem("Open");
        file.add(open);
        bar.add(file);
        select=new JMenuItem("Select All");
        select.addActionListener(this);
        file.add(select);
        copy=new JMenuItem("Copy");
        file.add(copy);
        cut=new JMenuItem("Cut");
        file.add(cut);
        paste=new JMenuItem("Paste");
        file.add(paste);
        open.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        cut.addActionListener(this);

        bar.setBounds(0,0,400,20);
        add(bar);
        area=new JTextArea(400,400);
        area.setBounds(0,20,400,400);
        add(area);


        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);


    }
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource()==open)
        {
            chooser=new JFileChooser();
            int j=chooser.showOpenDialog(null);

            if(j==JFileChooser.APPROVE_OPTION)
            {
                File f=chooser.getSelectedFile();
                String path=f.getPath();
                try{
                    BufferedReader br=new BufferedReader(new FileReader(path));
                    String s1="",s2="";
                    while((s1=br.readLine())!=null){
                        s2+=s1+"\n";
                    }
                    area.setText(s2);
                    br.close();
                }

                catch(Exception e)
                {
                    e.printStackTrace();

                }}}

        else if(event.getSource()==copy)
        {
            area.copy();
        }
        else if(event.getSource()==cut)
        {
            area.cut();
        }
        else if(event.getSource()==paste)
        {
            area.paste();
        }
        else if(event.getSource()==select)
        {
            area.selectAll();
        }
    }


    public static void main(String args[])
    {
        new JavaJFileChooserExample();
    }

}
]

Outputs:

Above image is when the UIManger is set to WindowsClassicLookAndFeel .If we use default Java Look this how JFileChooser will look like.
Java Applications LOOk and Feel


Simple notepad java application



Related Tutorials

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to use Java JFileChooser
How to use Java JFileChooser
How to use Java JFileChooser,
https://1.bp.blogspot.com/-qYmAfjLnai4/WOIxw936IVI/AAAAAAAAHbo/_ZxWxIwKXi0FGqf_Yn1CC-ABrKlsrLKSACLcB/s200/java-jfilechooser-example.PNG
https://1.bp.blogspot.com/-qYmAfjLnai4/WOIxw936IVI/AAAAAAAAHbo/_ZxWxIwKXi0FGqf_Yn1CC-ABrKlsrLKSACLcB/s72-c/java-jfilechooser-example.PNG
Java Codes
http://www.download-all.net/2017/04/how-to-use-java-jfilechooser.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/04/how-to-use-java-jfilechooser.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy