File Save Dialog Box

Posted by Fizzgig on Wed, 10 Jul 2019 23:34:15 +0200

File selection dialog box includes several categories of opening and saving files and customizing them. The file saving dialog box is often used in all kinds of editor modules, such as the file saving dialog box of Notebook program, the file saving dialog box of drawing program and the file saving dialog box of Photoshop program. This example will display the file save dialog box through Java code, and readers can apply it to their own projects. The running effect of the example is shown in the figure. Enter the edited text, then select the File /"Save" command, and pop up the Save dialog box, as shown in the figure. This example also uses the method of JFileChooser class to open the file dialog box, but this example opens the file save dialog box instead of the file open dialog box. Please pay attention to the title of the dialog box and the name of the button. The method of displaying the file save dialog box used in the example is as follows:

public int showSaveDialog(Component parent)
                   throws HeadlessException

Description of parameters Parent: parent form object. Return value: The int constant corresponding to the user's operation in the file opening dialog box. (1) Create the form class FileSaveDialog in the project. Add text fields and menu bars to the form, and then add "Save" and "Exit" menu items to the menu bar. (2) Write the event processing method of "save" menu item, create a file selector in this method, then call its method to display the file, open the dialog box, and get the file selected by the user, and then save the text in the text field to the file selected by the user. The code is as follows:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class FileSaveDialog extends JFrame {

    private JPanel contentPane;
    private JTextArea textArea;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FileSaveDialog frame = new FileSaveDialog();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FileSaveDialog() {
        setTitle("\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6307\u5B9A\u6570\u636E\u5907\u4EFD");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 291);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menu = new JMenu("\u6587\u4EF6");
        menuBar.add(menu);

        JMenuItem menuItem = new JMenuItem("\u4FDD\u5B58");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_menuItem_actionPerformed(e);
            }
        });
        menu.add(menuItem);

        JMenuItem menuItem_1 = new JMenuItem("\u9000\u51FA");
        menuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_menuItem_1_actionPerformed(e);
            }
        });
        menu.add(menuItem_1);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        textArea = new JTextArea();
        textArea.setFont(new Font("Song style", Font.PLAIN, 14));
        textArea.setLineWrap(true);
        textArea.setTabSize(4);
        scrollPane.setViewportView(textArea);
    }

    protected void do_menuItem_actionPerformed(ActionEvent e) {
        String text = textArea.getText();// Get user input
        if (text.isEmpty()) {// Storage operation of filtering empty text
            JOptionPane.showMessageDialog(this, "No text needs to be saved");
            return;
        }
        JFileChooser chooser = new JFileChooser();// Create a File Selector
        int option = chooser.showSaveDialog(this);// Open the File Save dialog box
        if (option == JFileChooser.APPROVE_OPTION) {// Processing File Save Operation
            File file = chooser.getSelectedFile();// Get the file selected by the user
            try {
                FileOutputStream fout = new FileOutputStream(file);// Create the output stream of the file
                fout.write(text.getBytes());// Save text to a file
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    protected void do_menuItem_1_actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

Psychological insight: Specify the parent form of the file selection dialog box. As long as the dialog box is a dialog box, you should specify a parent form as far as possible. The file selection dialog box is the same. When the dialog box is opened, all event operations of the parent form will be blocked or intercepted. Before the user completes the business operations in the dialog box, it is not allowed to operate the main form. If the parent form is specified as the NULL value when the file selection dialog box is displayed, the dialog box will select the default main form.

Topics: Java