jfilechooser

save file with JFileChooser save dialog

允我心安 提交于 2019-11-30 16:10:52
I have written a Java program that opens all kind of files with a JFileChooser. Then I want to save it in another directory with the JFileChooser save dialog, but it only saves an empty file. What can I do for saving part? Thanks. JFileChooser just returns the File object, you'll have to open a FileWriter and actually write the contents to it. E.g. if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); FileWriter fw = new FileWriter(file); fw.write(contents); // etc... } Edit: Assuming that you simply have a source file and destination file and want to copy the

InterruptedException after cancel file open dialog - 1.6.0_26

夙愿已清 提交于 2019-11-30 09:03:41
The output from the code that follows is: java.vendor Sun Microsystems Inc. java.version 1.6.0_26 java.runtime.version 1.6.0_26-b03 sun.arch.data.model 32 os.name Windows XP os.version 5.1 os.arch x86 Input selection cancelled by user. Exception while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d.Disposer.run(Unknown Source) at java.lang.Thread.run(Unknown Source) The following code shows the

JFileChooser to open multiple txt files

百般思念 提交于 2019-11-30 07:52:44
问题 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? 回答1: 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

Setting file creation timestamp in Java

这一生的挚爱 提交于 2019-11-30 06:43:56
I know setting the creation timestamp doesn't exist in Java because Linux doesn't have it, but is there a way to set a file's (Windows) creation timestamp in Java? I have a basic modification timestamp editor I made right here. import java.io.*; import java.util.*; import java.text.*; import javax.swing.*; public class chdt{ static File file; static JFrame frame = new JFrame("Input a file to change"); public static void main(String[] args) { try{ final JFileChooser fc = new JFileChooser(); fc.setMultiSelectionEnabled(false); //BufferedReader bf = new BufferedReader(new InputStreamReader(System

how do I make jfilechooser only accept .txt

筅森魡賤 提交于 2019-11-30 02:48:42
I trying to save my contact in my table but filechosser always setit to all file. is there way I can set it to accept .txt only and make it default or the only option. 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

save file with JFileChooser save dialog

佐手、 提交于 2019-11-29 23:36:26
问题 I have written a Java program that opens all kind of files with a JFileChooser. Then I want to save it in another directory with the JFileChooser save dialog, but it only saves an empty file. What can I do for saving part? Thanks. 回答1: JFileChooser just returns the File object, you'll have to open a FileWriter and actually write the contents to it. E.g. if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); FileWriter fw = new FileWriter(file); fw.write(contents); /

InterruptedException after cancel file open dialog - 1.6.0_26

微笑、不失礼 提交于 2019-11-29 14:14:21
问题 The output from the code that follows is: java.vendor Sun Microsystems Inc. java.version 1.6.0_26 java.runtime.version 1.6.0_26-b03 sun.arch.data.model 32 os.name Windows XP os.version 5.1 os.arch x86 Input selection cancelled by user. Exception while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d

How do you remove the Ctrl+C action on a JFileChooser?

China☆狼群 提交于 2019-11-29 13:38:07
I'm embedding a JFileChooser in my program in my own frame with other custom components in the frame. Here's a design of my app as it may help visualize my issues: If you can't tell, the lists directly under the JFrame titles are JFileChoosers . The way this is supposed to work is you assign shortcuts to destinations and then when you press those shortcut keys, the selected file moves to the destination. My strategy for doing this is to assign the shortcut to a javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW scope of the InputMap of the entire frame. But what's annoying is that something (I

FileFilter for JFileChooser

青春壹個敷衍的年華 提交于 2019-11-29 13:13:45
I want to restrict a JFileChooser to select only mp3 files. But, the following code allows all file types: FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3"); fileChooser.addChoosableFileFilter(filter); fileChooser.showOpenDialog(frame); File file = fileChooser.getSelectedFile(); Try and use fileChooser.setFileFilter(filter) instead of fileChooser.addChoosableFileFilter(filter); Try: FileFilter filter = new FileNameExtensionFilter("My mp3 description", "mp3"); The first argument is simply a description of the FileNameExtensionFilter - and since the second argument is var args,

How to act upon hitting “Enter” when on “Cancel” button in JFileChooser?

旧城冷巷雨未停 提交于 2019-11-29 12:28:06
I have a JFileChooser in a JFrame . I've added an ActionListener to the JFileChooser so that the "Cancel" button works when clicked. I can also tab to the "Cancel" button, but when I then hit the "Enter" key, nothing happens (i.e., the ActionListener isn't called with the event command JFileChooser.CANCEL_SELECTION ). What must I do with the JFileChooser so that hitting the "Enter" key when on the "Cancel" button is equivalent to clicking on the "Cancel" button? Here's a simple example of the (mis)behavior I'm seeing: import java.awt.event.ActionEvent; import java.awt.event.ActionListener;