Steve
Steve

Reputation: 17

jFileChooser to save file of selected tab

Ok so I have a text editor made that can so far create new files and open files using jFileChooser.

What I am trying to do is get the saving of files to work. Everytime you add or open a few file it adds a new tab to the tabbedpane and the name will be either file 1 etc or the name of the file opened.

When you click the save button the save dialog opens up

  int returnVal = fileChooser.showSaveDialog(this);

I want the name on the tab to be inserted to the name of file field.

Also how do I make a file of the current selected tabs textarea? I have tried this but its a no go:

  int index = tabbedPane.getSelectedIndex();
  Component c = tabbedPane.getComponentAt(index);

  JTextArea a = (JTextArea) c;
  System.out.println(a.getText());

 File file = new File(a.getText());
 fileChooser.setSelectedFile(file);

So I need to make a file of the string in the textArea I guess.

Upvotes: 0

Views: 1248

Answers (3)

Guillaume Polet
Guillaume Polet

Reputation: 47637

Following up @Andrew's answer, here is a snippet illustrating what he meant. I took the liberty to rather use a OutputStreamWriter than a FileWriter because this allows you to choose the charset used to write the file, which is something that you usually want to control and not rely on the "random" default platform charset.

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestTextArea {

    private JTextArea textArea;
    private JButton save;

    protected void initUI() {
        JFrame frame = new JFrame(TestTextArea.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textArea = new JTextArea(24, 80);
        save = new JButton("Save to file");
        save.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                saveToFile();
            }
        });
        frame.add(new JScrollPane(textArea));
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(save);
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setSize(500, 400);
        frame.setVisible(true);
    }

    protected void saveToFile() {
        JFileChooser fileChooser = new JFileChooser();
        int retval = fileChooser.showSaveDialog(save);
        if (retval == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            if (file != null) {
                if (!file.getName().toLowerCase().endsWith(".txt")) {
                    file = new File(file.getParentFile(), file.getName() + ".txt");
                }
                try {
                    textArea.write(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                    Desktop.getDesktop().open(file);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextArea().initUI();
            }
        });
    }
}

Upvotes: 3

MadProgrammer
MadProgrammer

Reputation: 347332

You need to, some how associate the File that was opened with the tab. That way, you can look up the File associated based on the selected tab.

Something like HashMap<Component, File> for example

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

An easy way is to use JTextComponent.write(Writer). JTextArea extends JTextComponent.

For the Writer use a FileWriter.

Upvotes: 2

Related Questions