jFileChooser保存选定选项卡的文件 [英] jFileChooser to save file of selected tab

查看:79
本文介绍了jFileChooser保存选定选项卡的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我制作了一个文本编辑器,到目前为止,它可以使用jFileChooser创建新文件和打开文件.

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

我想做的就是保存文件才能正常工作. 每次添加或打开一些文件时,它都会在选项卡窗格中添加一个新选项卡,名称将为文件1等或已打开文件的名称.

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);

所以我需要在我猜的textArea中创建一个字符串文件.

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

推荐答案

在遵循@Andrew的回答之后,以下是一个片段,说明了他的意思.我自由选择使用OutputStreamWriter而不是FileWriter,因为这允许您选择用于写入文件的字符集,这通常是您要控制的,而不是依赖于随机"默认平台字符集

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

这篇关于jFileChooser保存选定选项卡的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆