R
R
random2014-10-19 10:38:32
Java
random, 2014-10-19 10:38:32

How can I make it so that after pressing the button, it asks the user where to save the file?

private void actionAdd() {
        URL verifiedUrl = verifyUrl(addTextField.getText());
        if (verifiedUrl != null) {
            tableModel.addDownload(new Downloader(verifiedUrl));
            addTextField.setText(""); // reset add text field
        } else {
            JOptionPane.showMessageDialog(this,
                    "Invalid Download URL", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Power, 2014-10-19
@demon123

To show a window with a choice of where to save a file, use JFileChooser.showSaveDialog .

R
random, 2014-10-20
@demon123

private void actionAdd() {
        JFrame parentFrame = new JFrame();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Specify a file to save");
        URL verifiedUrl = verifyUrl(addTextField.getText());
        
        if (verifiedUrl != null) {
            int userSelection = fileChooser.showSaveDialog(parentFrame);
            if (userSelection == JFileChooser.APPROVE_OPTION) {


              // что здесь написать,чтобы сохранял файл куда я указал?


                tableModel.addDownload(new Download(verifiedUrl));
                addTextField.setText(""); // reset add text field
            }
            
        } else {
            JOptionPane.showMessageDialog(this,
                    "Invalid Download URL", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question