Saving with a JFileChooser(使用 JFileChooser 保存)
问题描述
I am using a JFileChooser
and the showSaveDialoge()
and setSelectionMode(JfileChooser.DIRECTORIES_ONLY)
to set where a preselected file will be saved and what it will be called.
I want the user to be able to choose the name the new version, and where to put it. How do I go about this? I also wish to choose a default name.
I hope the codes below implemented inline with your question requirements. The criteria in your question are answered in the code comment. If you need clarification, please let me know.
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class MyFileChooser extends JPanel implements ActionListener
{
JButton saveButton;
JTextArea content;
JFileChooser jfc;
private boolean docUpdate = false;
private String default_filename = "default.txt";
private String default_directory = "/home/foo/workspace";
private int version = 0;
public MyFileChooser()
{
super(new BorderLayout());
content = new JTextArea(5, 20);
content.setMargin(new Insets(5, 5, 5, 5));
content.getDocument().addDocumentListener(new DocumentListener()
{
@Override
public void removeUpdate(DocumentEvent e)
{
System.out.println("remove update");
docUpdate = true;
}
@Override
public void insertUpdate(DocumentEvent e)
{
System.out.println("insert update");
docUpdate = true;
}
@Override
public void changedUpdate(DocumentEvent e)
{
System.out.println("changed update");
docUpdate = true;
}
});
JScrollPane logScrollPane = new JScrollPane(content);
jfc = new JFileChooser();
/**
* Set a initial default directory and a initial default filename here.
*/
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setCurrentDirectory(new File(default_directory));
jfc.setSelectedFile(new File(default_filename));
jfc.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt)
{
if (docUpdate == true)
{
/**
* User can use the suggested filename or remove the suggested,
* and enter a new filename.
* Here, we set to a new directory and of cause, user can select
* what is the directory he want.
*/
jfc.setCurrentDirectory(new File("/home/foo"));
jfc.setSelectedFile(new File("default" + version + ".txt"));
}
}
});
saveButton = new JButton("Save a File...");
saveButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(saveButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = jfc.showSaveDialog(MyFileChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = jfc.getSelectedFile();
// save the file.
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(content.getText());
bw.flush();
}
catch (IOException e1)
{
e1.printStackTrace();
}
version++;
}
else
{
System.out.println("Save command cancelled by user. ");
}
content.setCaretPosition(content.getDocument().getLength());
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyFileChooser());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
}
这篇关于使用 JFileChooser 保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JFileChooser 保存
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01