jfilechooser

JFileChooser, want to lock it to one directory

僤鯓⒐⒋嵵緔 提交于 2019-11-29 11:58:44
I have this program where u can download files and i want the JFileChooser to be locked to one folder(directory) so that the user cant browse anything else. He can only choose files from for example the folder, "C:\Users\Thomas\Dropbox\Prosjekt RMI\SERVER\". I have tried so search but did not find anything. The code I have is: String getProperty = System.getProperty("user.home"); JFileChooser chooser = new JFileChooser(getProperty + "/Dropbox/Prosjekt RMI/SERVER/"); //opens in the directory "//C:/Users/Thomas/Dropbox/Project RMI/SERVER/" int returnVal = chooser.showOpenDialog(parent); if

How to save file using JFileChooser in Java? [closed]

跟風遠走 提交于 2019-11-29 11:51:59
问题 I have following code. It saves file but with empty content. What's wrong with it? 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(); } } } 回答1: If you're

JFileChooser embedded in a JPanel

情到浓时终转凉″ 提交于 2019-11-29 10:04:36
I am writing a java program that needs a file open dialog. The file open dialog isn't difficult, I'm hoping to use a JFileChooser . My problem is that I would like to have a dual pane JFrame (consisting of 2 JPanels ). The left panel would have a JList , and the right panel would have a file open dialog. When I use JFileChooser.showOpenDialog() this opens the dialog box above all other windows, which isn't what I want. Is there any way to have the JFileChooser (or maybe another file selection dialog) display inside a JPanel and not pop-up above it? Here is the code that I've tried, at this

Change the location of the accessory component in a filechooser

南楼画角 提交于 2019-11-29 08:39:57
I have a problem in changing the location of the accessory component in a filechooser. I customized a save file dialog by putting a check box in the accessory component of the file chooser. But the position of the check box is not good, it is really ugly. Can I move the accessory component to be underneath the file pane? How to do it? Or if you have other solution to do the same thing, also welcome. Thank you guys. I using following code to add the check box: JFileChooser fc = new JFileChooser(file) JPanel accessory = new JPanel(); JCheckBox isOpenBox = new JCheckBox("Open file after saving");

NPE in Win32ShellFolder2.access when creating new JFileChooser as Local System Account in Windows 7

倖福魔咒の 提交于 2019-11-29 08:04:07
I have written unit tests for a Swing GUI that creates JFileChooser. Since the unit tests are run on a build server as a service, the unit tests need to run as the local system account. However, when the unit tests try to create a new JFileChooser, they throw a NullPointerException. I've reduced the problem to that of running the following main class as local system account (NOT THE REAL CODE) package com.example.mcgr; import javax.swing.*; import java.io.IOException; public class FileChooserAsSystem { public static void main(String[] args) throws IOException { SwingUtilities.invokeLater(new

Opening files with JFileChooser

大城市里の小女人 提交于 2019-11-29 07:14:22
As a little side project I'd thought it would cool to make a text editor. I'm currently stuck on opening files. This is my code for opening the file( e is an ActionEvent , open is a JMenuItem ): else if (e.getSource() == open) { JFileChooser choice = new JFileChooser(); int option = choice.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { try{ Scanner scan = new Scanner(new FileReader((open).getSelectedFile().getPath())); } } } The try block is giving me the trouble. Eclipse is saying that getSelectedFile() is undefined for type JMenuItem . It also appears to be undefined for

JFileChooser to open multiple txt files

安稳与你 提交于 2019-11-29 05:34:50
How can I use JFileChooser to open two text files and after I selected these files, I want to compare them, show on the screen etc. Is this possible? You can have your JFileChooser select multiple files and return an array of File objects instead of one JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser.showOpenDialog(frame); File[] files = chooser.getSelectedFiles(); The method showOpenDialog(frame) only returns once you click the ok button EDIT So do this: JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser

JOptionPane YES/No Options Confirm Dialog Box Issue

早过忘川 提交于 2019-11-28 16:38:07
I've created a JOptionPane and it only has two buttons YES_NO_OPTION . After JOptionPane.showConfirmDialog pops out , I want to click YES BUTTON to continue opening the JFileChooser and if I clicked NO BUTTON it should cancel the operation. It seems pretty easy but I'm not sure where is my mistake. Code Snippet: 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 ==

Getting multiple files from JFileChooser

早过忘川 提交于 2019-11-28 12:47:53
In a GUI app that I am working on, I require to select multiple files but instead of directly opening it with the File chooser I first need to add all required files in a Selected list (so that instead of selecting files again and again from different directories I can select them all at a time and then open all the files added to that list). Moreover I should also be able to remove multiple files from those present in that Selected File list too. Is that possible with JFileChooser or do I need to design one as per my requirements? What you are looking for is not a standard feature, but you

adjust selected File to FileFilter in a JFileChooser

℡╲_俬逩灬. 提交于 2019-11-28 12:07:06
I'm writing a diagram editor in java. This app has the option to export to various standard image formats such as .jpg, .png etc. When the user clicks File->Export, you get a JFileChooser which has a number of FileFilter s in it, for .jpg , .png etc. Now here is my question: Is there a way to have the extension of the default adjust to the selected file filter? E.g. if the document is named "lolcat" then the default option should be "lolcat.png" when the png filter is selected, and when the user selects the jpg file filter, the default should change to "lolcat.jpg" automatically. Is this