我创建了一个JOptionPane
它只有两个按钮YES_NO_OPTION
.
后JOptionPane.showConfirmDialog
弹出,我想点击YES BUTTON
继续打开JFileChooser
,如果我点击NO BUTTON
它应该取消操作.
这似乎很容易,但我不确定我的错误在哪里.
代码片段:
if (textArea.getLineCount() >= 1) { //The condition to show the dialog if there is text inside the textArea
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if (dialogButton == JOptionPane.YES_OPTION) { //The ISSUE is here
JFileChooser saveFile = new JFileChooser();
int saveOption = saveFile.showSaveDialog(frame);
if(saveOption == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter fileWriter = new BufferedWriter(new …
Run Code Online (Sandbox Code Playgroud) 在JFileChooser
似乎缺少该FEATURE:一种方式保存文件(通常被选中,这样,当用户开始输入将被替换的东西)时,建议的文件名.
有没有解决的办法?
我想在java中创建一个"打开"和"保存"对话框.我想要的一个例子如下图所示:
打开:
保存:
我该怎么做呢?
我试图将我的联系人保存在我的表中但是filechosser总是设置为所有文件.有没有办法我可以设置它只接受.txt并使其成为默认或唯一选项.
savecontact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filesave = new JFileChooser();
int returnVal = filesave.showSaveDialog(Main.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = filesave.getSelectedFile();
PrintWriter os = new PrintWriter(file);
os.println("");
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col) + "\t");
}
os.println("");
os.println("");
for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col));
os.print(": ");
os.println(table.getValueAt(row, col));
}
os.println("");
}
os.close(); …
Run Code Online (Sandbox Code Playgroud) 我需要知道如何在java中获取"浏览文件夹"对话框.我知道SWT.但是我需要在摇摆中做什么?这有什么解决方案吗?
[当我们开始使用eclipse时,它会要求选择工作空间.我们当时可以看到浏览文件夹对话框]提前谢谢.
我想将用户限制在目录及其子目录中,但"父目录"按钮允许他们浏览到任意目录.
我应该怎么做呢?
我要求对当前使用JFileChooser的小applet进行一些更改.
其中一个主要的抱怨是文件选择器很难使用,因为它的行为与本机小部件不同,特别是对于导航到根级别.
所以,知道这个以及JFileChooser遭受的所有其他问题(比如在Windows上缓存的zip文件......),我想知道java世界中存在一个可行的替代方案.
当然,有SWT使用本机窗口小部件,但是将applet大小增加25并不是一个真正的选择.那么,文件选择器是否有更好的纯java实现?
我似乎对我的文件选择器对话的非常简单的实现有一个问题,这需要我每次都要最小化Netbeans才能到达它,并且特别是现在通过测试它变得非常令人沮丧.
我已经在网上看到了一些解决方案,包括SO,但似乎没有一个可以做到这一点,而其他一些解决方案对我目前的水平看起来非常冗长和复杂.
private void fileSearch() {
JFileChooser fileSelect = new JFileChooser();
int returnVal = fileSelect.showOpenDialog(null);
String pathToFile;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileSelect.getSelectedFile();
pathToFile = file.getAbsolutePath();
try {
P.binaryFileToHexString(pathToFile);
} catch (Exception e) {
System.out.print("Oops! there was an error there..." + e);
}
System.out.println("\nYou chose to open this file: " + file.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
我的一些尝试包括使用;
.requestFocus();
.requestFocusInWindow();
.setVisible();
Run Code Online (Sandbox Code Playgroud)
我可以设置一个特定的属性/方法来解决问题吗?
如何使用JFileChooser打开两个文本文件,在我选择这些文件后,我想比较它们,在屏幕上显示等等.这可能吗?
我有以下代码.它保存文件但内容为空.它出什么问题了?
public void saveMap() {
String sb = "TEST CONTENT";
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("/home/me/Documents"));
int retrival = chooser.showSaveDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt");
fw.write(sb.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)