Save file/open file dialog box, using Swing & Netbeans GUI editor

111,082

Solution 1

I have created a sample UI which shows the save and open file dialog. Click on save button to open save dialog and click on open button to open file dialog.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FileChooserEx {
    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new FileChooserEx().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

    private void createUI() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JButton saveBtn = new JButton("Save");
        JButton openBtn = new JButton("Open");

        saveBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser saveFile = new JFileChooser();
                saveFile.showSaveDialog(null);
            }
        });

        openBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser openFile = new JFileChooser();
                openFile.showOpenDialog(null);
            }
        });

        frame.add(new JLabel("File Chooser"), BorderLayout.NORTH);
        frame.add(saveBtn, BorderLayout.CENTER);
        frame.add(openBtn, BorderLayout.SOUTH);
        frame.setTitle("File Chooser");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Solution 2

I think you face three problems:

  1. understanding the FileChooser
  2. writing/reading files
  3. understanding extensions and file formats

ad 1. Are you sure you've connected the FileChooser to a correct panel/container? I'd go for a simple tutorial on this matter and see if it works. That's the best way to learn - by making small but large enough steps forward. Breaking down an issue into such parts might be tricky sometimes ;)

ad. 2. After you save or open the file you should have methods to write or read the file. And again there are pretty neat examples on this matter and it's easy to understand topic.

ad. 3. There's a difference between a file having extension and file format. You can change the format of any file to anything you want but that doesn't affect it's contents. It might just render the file unreadable for the application associated with such extension. TXT files are easy - you read what you write. XLS, DOCX etc. require more work and usually framework is the best way to tackle these.

Solution 3

saving in any format is very much possible. Check following- http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

2ndly , What exactly you are expecting the save dialog to work , it works like that, Opening a doc file is very much possible- http://srikanthtechnologies.com/blog/openworddoc.html

Share:
111,082
Pratik Anand
Author by

Pratik Anand

Updated on July 09, 2022

Comments

  • Pratik Anand
    Pratik Anand almost 2 years

    I am a beginner to Java. I am making a simple text editor in netbeans 7(.3) IDE, using its GUI editor. The main problem I face in it is that I can't make it to save/open the file. I have created the "save" button. When I drop the file chooser, it comes as a normal open file dialog box embedded in the java window with no functionality at all. I have also tried creating a new jFileChooser when the save button is clicked (in the Source view), but it doesn't work.

    In a nutshell, I need a simple open/save dialog box. When the "Save" button is pressed, the save dialog box opens and saves the file wherever the user chooses with whatever name and .rtf or .txt extension. (P.S.: is it possible to save a file in .docx or .doc in Java?)
    When the "Open" btn is pressed, it opens a file in .rtf or .txt (again, is it possible to open .docx or .doc in Java?) through the file chooser.

        private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        JFileChooser saveFile = new JFileChooser();
        if saveFile.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION {
            File xyz = saveFile.getSelectedFile();
        }
    }
    

    Code is here: https://docs.google.com/file/d/0B766zz1iJ1LRN2lGRjNtM29vN2M/edit?usp=sharing

  • Alaa Othman
    Alaa Othman over 9 years
    I mean the digalog box style on windows operation system as this code makes a digalog box like the one on Mac.
  • Alvaro Pedraza
    Alvaro Pedraza over 6 years
    @Amarnath, what file are you saving with the save button? How do I tell the ActionListener to open the dialog saving a specific file? Can I specify to JFileChooser a default folder to open? Thanks in advance for your time and congratulations for your answer