JFileChooser看起来不像本机小部件.我似乎记得阅读一些黑客让它看起来像本机小部件,但搜索它知道我似乎无法再找到它我遇到的帖子建议使用java.awt.FileChooser但该类似乎没有分布.如何让JFileChooser看起来像本机小部件?
好吧,这个真的很奇怪.每次我的应用程序第一次打开JFileChooser时,它都会抛出IOException,然后某些图标无法正常显示.
java.io.IOException
at sun.awt.image.GifImageDecoder.readHeader(GifImageDecoder.java:265)
at sun.awt.image.GifImageDecoder.produceImage(GifImageDecoder.java:102)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
Run Code Online (Sandbox Code Playgroud)
现在,当我深入研究错误时,它似乎在尝试读取标题时在一个图标上,它只检索前8个字节,这是不够的.我已经检查了图标文件,它们似乎都没问题.我试图用另一个在此错误之前正确加载的图标文件覆盖图标文件,但同样的事情.
打破这个错误时,这是我的堆栈:
Daemon Thread [Image Fetcher 0] (Suspended (exception IOException))
GifImageDecoder.readHeader() line: 265 [local variables unavailable]
GifImageDecoder.produceImage() line: 102 [local variables unavailable]
ByteArrayImageSource(InputStreamImageSource).doFetch() line: 246
ImageFetcher.fetchloop() line: 172
ImageFetcher.run() line: 136 [local variables unavailable]
Run Code Online (Sandbox Code Playgroud)
这是挖掘GifImageDecoder实例时的变量值.
source ByteArrayImageSource (id=272)
awaitingFetch false
consumers null
decoder GifImageDecoder (id=271)
decoders GifImageDecoder (id=271)
imagedata (id=307)
[0] 71
[1] 73
[2] 70
[3] 56
[4] 57
[5] 97
[6] 16
[7] 13
[8] 10 …Run Code Online (Sandbox Code Playgroud) (提前致谢!如果您需要更多信息,请告诉我.底部的示例代码.)
我正试图解决的问题:
我试图让这个JFileChooser对象只显示目录(而不是文件),通过使用在accept(文件文件)重写方法中具有此功能的javax.swing.filechooser.FileFilter对象:return file.isDirectory();.但是,至少在我的Mac上,它似乎并不会阻止文件与目录一起显示(它确实可以防止在不使用setFileSelectionMode()方法的情况下选择文件).
题
我错过了什么吗?如果没有,有没有人曾经遇到过这个?
我的理解/假设:
setFileFilter()方法时,会发生魔法.setSelectionMode( JFileChooser.DIRECTORIES_ONLY );码
import java.io.File;
import javax.swing.filechooser.FileFilter;
// inside a method that's adding this to a JPanel
_fileChooser = new JFileChooser( "." );
_fileChooser.setControlButtonsAreShown( false );
_fileChooser.setFileFilter( new FolderFilter() );
// _fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
_panelMidLeft.add( _fileChooser );
// an inner class, defined somewhere else in the class
private class FolderFilter extends javax.swing.filechooser.FileFilter {
@Override
public boolean accept( File file ) {
return file.isDirectory();
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 对于用于将PDF与Apache PDFBox合并的简单Swing应用程序,我使用a JFileChooser来选择一个或多个PDF文件并将其添加到a JList.到目前为止没问题.
令我困扰的是,当我单击按钮再次添加另一个文件/文件时,之前的选择仍然存在于JFileChooser中,我不希望这样,选择应该最初为空.
我试过这个,但既不起作用也不抛出异常:
pdfFileChooser.setSelectedFile(null);
Run Code Online (Sandbox Code Playgroud)
这是相关代码:
pdfFileChooser.setAcceptAllFileFilterUsed(false);
pdfFileChooser.setMultiSelectionEnabled(true);
pdfFileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File arg0) {
return arg0.getName().endsWith(".pdf");
}
@Override
public String getDescription() {
return "*.pdf";
}
} );
JButton btnAddFile = new JButton("Add file");
btnAddFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(pdfFileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
addFileToList(pdfFileChooser.getSelectedFiles());
pdfFileChooser.setSelectedFile(null);
}
}
});
private void addFileToList(File[] filesToAdd) {
if((filesToAdd != null) && (filesToAdd.length > 0)) {
DefaultListModel model = (DefaultListModel)listFiles.getModel();
for(File file : filesToAdd) {
if(!model.contains(file)) …Run Code Online (Sandbox Code Playgroud) 我希望JFileChooser保存对话框的文件名字段中的文本只选择文件名而不是扩展名.
我目前有这个:

并希望它看起来像这样:

这是一个简单的更改,但在我看来,这使得保存文件更容易,因为用户可以立即开始输入文件名而不会意外删除扩展名.
我知道如果缺少扩展名,我可以强行添加扩展名,但我宁愿不这样做,因为扩展名不是强制性的,我觉得不应该强制执行.
那么,有什么办法可以实现这个目标吗?
我是eclipse窗口构建器的新手,想要用它创建一个简单的GUI.我想添加JFileChooser,但它不能在调色板中找到,即使它是一个Swing组件.我搜索了我的组件,但没有文件选择器.
如何添加JFileChooser到Eclipse Window Builder?
我正在使用Eclipse IDE,我正试图showSaveDialog(null)从另一个方法调用,而这个方法又是从main调用的,但是当我调用它时没有任何反应.
主要:
public static void main(String[] args) {
Main main = new Main();
main.pt = main.new PlayThread();
main.generateSound(getSamples(2), 500);
main.play(main.sound, 1);
if(new Scanner(System.in).nextLine().equals("save")){
System.out.println(main.save());
}
}
Run Code Online (Sandbox Code Playgroud)
调用之前的所有代码都main.save()可以正常工作,并且main.save()像这样开始:
public boolean save(){
System.out.println("Calling save");
try{
String saveName = getSaveTo("C:/");
System.out.println("I'm here now");
Run Code Online (Sandbox Code Playgroud)
虽然getSaveTo()看起来像:
public String getSaveTo(String def){
JFileChooser chooser = new JFileChooser();
System.out.println("Save called");
//chooser.setCurrentDirectory(new File(def));
int resp = chooser.showSaveDialog(null);
if(resp == JFileChooser.APPROVE_OPTION){
return chooser.getSelectedFile() + ".wav";
}else return null;
}
Run Code Online (Sandbox Code Playgroud)
在执行前几个函数后main …
我们软件的用户需要在我们的Java swing应用程序中浏览Windows 10上的网络共享,但是swing的JFileChooser默认情况下不具备此功能.
在这篇相关文章中 如何在JFileChooser中导航到网络主机? 使用ShellFolder(sun私有API)来设置JFileChooser的当前目录是一个不错的解决方案,我们在过去几年中一直使用这种方法进行一些修改而没有任何问题.
public static File getNetworkShareFolder( final File folder ) throws IllegalArgumentException {
final File file = new NonCanonicalizingFile( folder.getPath() );
if( isNetworkShareFolder( file ) ) { // assume Win32ShellFolderManager2 will be present
try {
// JRE-13272 -PRIVATE API that must eventually be swapped for Java 9 alternative
// Using reflection because Win32ShellFolderManager2 may not exist in rt.jar on Linux build server
final Class win32ShellMgr = Class.forName( "sun.awt.shell.Win32ShellFolderManager2" );
// get static creation method from class, …Run Code Online (Sandbox Code Playgroud) 我们如何设置JFileChooser窗口的位置,我试过setLocation()和setBounds() 方法,但它不起作用.