Opening files with JFileChooser

jfilechooser save file
open jfilechooser on button click
jfilechooser select directory
jfilechooser create new file
java file dialog
jfilechooser download file
image file chooser in java
jfilechooser filter

As a little side project I'd thought it would cool to make a text editor. I'm currently stuck on opening files. This is my code for opening the file(e is an ActionEvent, open is a JMenuItem):

else if (e.getSource() == open) {
        JFileChooser choice = new JFileChooser();
        int option = choice.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            try{
                Scanner scan = new Scanner(new FileReader((open).getSelectedFile().getPath()));
            }
        }

    }

The try block is giving me the trouble. Eclipse is saying that getSelectedFile() is undefined for type JMenuItem. It also appears to be undefined for MenuItems as well. Is there another way to approach this, or another method that works the same?

You need to call getSelectedFile() on the JFileChooser once it has returned, so change your code to:

choice.getSelectedFile()

Opening files with JFileChooser, Bringing up a standard open dialog requires only two lines of code: //Create a file chooser final  Show simple open file dialog using JFileChooser Add required import statements. The current directory is initial directory where Check if the user selects a file or not. If the user selects a file, And the whole code snippet is as follows. Nam Ha Minh is certified Java programmer (SCJP and

  private void selectfileActionPerformed(java.awt.event.ActionEvent evt) {                                           

    JFileChooser jFileChooser=new JFileChooser();
   StringBuffer buffer;
    buffer = new StringBuffer();
   int result= jFileChooser.showOpenDialog(this);
    if(result==JFileChooser.APPROVE_OPTION)
    {
        try {
            FileReader reader;
            reader = null;
            JOptionPane.showMessageDialog(this,"hii user clicked open sysytem");
            File file=jFileChooser.getSelectedFile();
            reader=new FileReader(file);
            int i=1;
            while(i!=-1)
            {
                i=reader.read();
                char ch=(char) i;
                buffer.append(ch);

            }

            notepad.setText(buffer.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}                                          

How to Use File Choosers (The Java™ Tutorials > Creating a GUI , of the file passed. JFileChooser j = new JFileChooser( new File( "C:\\Users\\pc\\​Documents\\New folder\\" ));. // Open the save dialog. j.showSaveDialog( null );  In this video, I will demo how to Open and Save File Using JFileChooser in Java Swing. To download all sources code for this demo please pay for me $5 at my PayPal Account

import java.awt.EventQueue;

public class FileChooser extends JFrame
{
    private JPanel contentPane;
    String filename;
   // main
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    FileChooser frame = new FileChooser();
                    frame.setVisible(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public FileChooser()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        // button to selct file
        JButton btnNewButton = new JButton("Select file");
        btnNewButton.setBounds(10, 2, 89, 23);
        contentPane.add(btnNewButton);
        // area to display file content
        final JTextArea textArea = new JTextArea();
        textArea.setBounds(10, 36, 414, 215);
        contentPane.add(textArea);
         // save button
        final JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {

                try
                {
                    FileWriter writer = new FileWriter(filename.replace(".",
                            "_out."));
                    BufferedWriter bwr = new BufferedWriter(writer);

                    bwr.write(textArea.getText());

                    bwr.close();
                    writer.close();
                    System.out.println(textArea.getText());
                } catch (Exception e)
                {
                    System.out.println("Error");
                }
            }
        });
        btnSave.setBounds(283, 2, 89, 23);
        contentPane.add(btnSave);
      // listen to button clicks
        btnNewButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION)
                {
                    File selectedFile = fileChooser.getSelectedFile();
                    filename = selectedFile.getAbsolutePath();

                    try
                    {
                        FileReader reader = new FileReader(filename);
                        BufferedReader br = new BufferedReader(reader);
                        textArea.read(br, null);
                        br.close();
                        System.out.println(textArea.getText());

                    } catch (Exception e)
                    {
                        System.out.println("Error");
                    }enter code here

                }

            }
    enter code here
        });

    }
}

Java Swing, 1. show*Dialog() – Open or save a file. Example of how to use the JFileChooser to get the absolute path for the file the  You can have your JFileChooser select multiple files and return an array of File objects instead of one JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser.showOpenDialog(frame); File[] files = chooser.getSelectedFiles();

Java Swing – JFileChooser example – Mkyong.com, In this video you will learn how to open any file in Java using JFileChooser in Eclipse. If you Duration: 6:05 Posted: Apr 17, 2017 Choosing a txt file with JFileChooser and making it appear on a JTextField just won't work. When I press open another window appears with JFileChooser to select the file again, and when I try to select the file again it just gives me "open command canceled by user" message. The program stops working and ends with an unknown exception.

Open a file in java using JFileChooser Eclipse Tutorial, We would like to know how to open Selected file with default application in JFileChooser. Answer. import java.awt.Desktop; import java.io.File; /*  Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories.

Open Selected file with default application in , File chooser dialog box is used for navigating the file system. With file chooser you can choose either JButton btn2 = new JButton("Show Open Dialog");. btn2. A FileChooser can be used to invoke file open dialogs for selecting single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog).

Comments
  • That method is on JFileChooser: choice.getSelectedFile();
  • It should be choice.getSelectedFile();
  • Is there anything better that JFileChooser. What if you want to make it look like it open in Microsoft Open box.
  • I recommend adding some descriptive text to explain the relevant sections of your code.
  • Adding a description would be helpful.
  • My bad copy the code and run it. it opens a text file and saves it out.