jfilechooser

JFileChooser select directory but show files

六眼飞鱼酱① 提交于 2019-11-28 11:57:05
I feel like there should be a simple way to do this but I can't figure it out. I have a JFileChooser that allows the user to select directories. I want to show all the files in the directories to give the user some context, but only directories should be accepted as selections (maybe the Open button would be disabled when a file is selected). Is there an easy way of doing this? Override the approveSelection() method. Something like: JFileChooser chooser = new JFileChooser( new File(".") ) { public void approveSelection() { if (getSelectedFile().isFile()) { // beep return; } else super

Save using JFileChooser with pre-populated file name?

百般思念 提交于 2019-11-28 11:07:12
问题 I am trying to make saving and loading easier for some GUIs that I've made, and I would like to be able to pre-populate a filename for the user on save. Getting the JFileChooser to point at a convenient directory is easy enough, but pre-populating the name doesn't seem so easy. Currently, my code is: JFileChooser f = new JFileChooser(); f.setSelectedFile(new File(generateName())); This actually appears to work at first: The filename is populated in the JFileChooser, but when clicking the save

making jfilechooser show image thumbnails

与世无争的帅哥 提交于 2019-11-28 09:27:21
I wanted to create a JFileChooser with thumbnail view of image files.So I subclassed FileView and in the method which creates ImageIcon did some scaling sothat thumbnail images are shown. However,the overall effect is that, the filechooser widget takes some time before opening a directory and showing thumbnails..In createImageIcon() below,I need to call new ImageIcon() twice-once with the image filepath and next with the resized image as constructor argument.I think this is what slows the widget . Is there a more efficient alternative?Any suggestions/pointers most welcome. thanks, mark public

FileFilter for JFileChooser

拥有回忆 提交于 2019-11-28 07:08:02
问题 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(); 回答1: Try and use fileChooser.setFileFilter(filter) instead of fileChooser.addChoosableFileFilter(filter); 回答2: If you want only mp3 files: import javax.swing.JFileChooser; import javax.swing

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

大城市里の小女人 提交于 2019-11-28 06:13:07
问题 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

JFileChooser, want to lock it to one directory

天涯浪子 提交于 2019-11-28 05:02:59
问题 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 "

JFileChooser embedded in a JPanel

自古美人都是妖i 提交于 2019-11-28 03:21:54
问题 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

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

假装没事ソ 提交于 2019-11-28 01:25:30
问题 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

Opening files with JFileChooser

别来无恙 提交于 2019-11-28 00:58:15
问题 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

How to use the default File Chooser for the operating system? java

我的梦境 提交于 2019-11-27 20:41:42
I was just wondering: how does Gmail use the Windows/Mac file chooser to upload files? Is there any way to do this in Java ? Personally, I don't like the way that the JFileChooser looks like, and I thought it would be better for my users to be able to use something that they're more used to. Tips anyone? Use the old java.awt.FileDialog instead: new java.awt.FileDialog((java.awt.Frame) null).setVisible(true); You can try using JFileChooser but setting the look and feel to be the platform look and feel: try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception