0

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.

1
  • 1
    Google for "Java IO tutorial". And look in the javadoc of JFileChooser for a method allowing to set the selected file. It's not hard to find.
    – JB Nizet
    Aug 24, 2013 at 20:45

3 Answers 3

3

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();
            }
        });
    }
}
2

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

For the Writer use a FileWriter.

2
  • Could you provide a snippet as I find it slight confusing!
    – Steve
    Aug 24, 2013 at 21:26
  • 2
    <sarcasm>Could you pay me?</sarcasm> Why not make an attempt, and if you do not succeed, post an SSCCE with copy/pasted stack trace or compilation error? Note that SO is not a 'help desk'. Aug 24, 2013 at 21:44
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.