关键词:
【中文标题】如何在 JFileChooser 中显示文件的默认系统图标?【英文标题】:How to display default system icon for files in JFileChooser? 【发布时间】:2013-07-11 21:21:28 【问题描述】:如何显示JFileChooser
中文件的默认系统图标?即JFileChooser
中文件的图标应该与桌面和资源管理器中出现的图标相同?
例如,NetBeans 图标在JFileChooser
中的显示与在桌面上显示的不同!
如何做到这一点?
【问题讨论】:
如果它有助于解决问题,请accept 回答。对于earlier questions 中的许多人来说,情况大致相同。当然,注意到该页面中没有答案的那个,总是可以选择删除它(或将其标记为删除)。 【参考方案1】:我们可以使用FileSystemView
类并通过在其中调用getFileSystemView()
静态方法来获取它的对象,然后使用getSystemIcon()
方法获取File
对象并返回它的图标。
FileSystemView
和 FileView
类存在于 javax.swing.filechooser
包中。
File
类在 java.io
包中。
注意: FileSystemView
不会扩展 FileView
。因此,您不能在 jf.setFileView()
中使用 FileSystemView
对象
JFileChooser jf=new JFileChooser();
jf.setFileView(new MyFileView());
jf.showOpenDialog(this);
class MyFileView extends FileView
public Icon getIcon(File f)
FileSystemView view=FileSystemView.getFileSystemView();
return view.getSystemIcon(f);
this
表示当前帧。假设写这段代码的类是JFrame
的子类
或者用一种简单的方式,
jf.setFileView(new FileView()
public Icon getIcon(File f)
return FileSystemView.getFileSystemView().getSystemIcon(f);
);
【讨论】:
【参考方案2】:@JavaTechnical 展示的方式是一种方式。这是另一种(更简单)的方法。将 GUI(或至少文件选择器)设置为原生 PLAF。例如
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class FileChooserIcons
public static void main(String[] args)
Runnable r = new Runnable()
@Override
public void run()
try
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
catch(Exception e)
e.printStackTrace();
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(20, 30, 20, 30));
JButton browse = new JButton("Show File Chooser");
final JFrame f = new JFrame("File Chooser");
ActionListener showChooser = new ActionListener()
JFileChooser jfc = new JFileChooser();
@Override
public void actionPerformed(ActionEvent e)
jfc.showOpenDialog(f);
;
browse.addActionListener(showChooser);
gui.add(browse);
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://***.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
;
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
当然,如果您有勇气,您可以创建一个自定义文件选择器,以 File Browser GUI 之类的开头。
【讨论】:
嗯。那很好!如果我想改变外观和感觉怎么办,说我喜欢 NimbusLookAndFeel! ;) @JavaTechnical “我喜欢 NimbusLookAndFeel” 你喜欢 bug? Nimbus 是有史以来开发的漏洞最多的 PLAF 之一。 ..这是在说些什么! +1 好的。顺便说一句,您对它的外观和定制方式有何看法? :) 嗯..我对此没有强烈的意见或感受。我从未尝试过自定义JFileChooser
(除了明显的“PLAF”)。
我热烈地记得那个程序和它的发展! 1+如何在 Java 9+ 中使用 JFileChooser 显示网络共享?
】如何在Java9+中使用JFileChooser显示网络共享?【英文标题】:HowtodisplaynetworksharesusingJFileChooserinJava9+?【发布时间】:2019-04-1009:42:44【问题描述】:我们软件的用户需要在我们的Javaswing应用程序中浏览Windows10上的网络共享,但是swi... 查看详情
java示例代码_:JFileChooser如何在文本字段中显示所选文件
java示例代码_:JFileChooser如何在文本字段中显示所选文件 查看详情
如何使用 JFileChooser 保存文件?
】如何使用JFileChooser保存文件?【英文标题】:HowtosavefileusingJFileChooser?【发布时间】:2011-02-0115:04:57【问题描述】:我的应用程序中有一个名为“另存为”的方法,它将我的应用程序在计算机上的图像保存到文件中。我使用JFile... 查看详情
java示例代码_如何在JFileChooser OpenDialog中使用两个文件扩展名之一的所有文件
java示例代码_如何在JFileChooser OpenDialog中使用两个文件扩展名之一的所有文件 查看详情
如何使 JFileChooser 显示除 .huff 文件之外的所有类型的文件 [重复]
】如何使JFileChooser显示除.huff文件之外的所有类型的文件[重复]【英文标题】:HowtomakeJFileChoosershoweverytypeoffilesexcept.hufffiles[duplicate]【发布时间】:2021-02-2703:59:12【问题描述】:我正在开发一个实现Huffman算法的Java(Swing)程序。它需... 查看详情
JFileChooser - 设置固定文件名
】JFileChooser-设置固定文件名【英文标题】:JFileChooser-settingafixedfilename【发布时间】:2012-08-3013:27:44【问题描述】:我正在制作一个类似于带有代码突出显示的文本编辑器的应用程序。当我尝试保存一些文本时,我希望在JFileChoos... 查看详情
如何从 JFileChooser(JAVA Swing) 中禁用文件操作、文件选择和过滤面板?
】如何从JFileChooser(JAVASwing)中禁用文件操作、文件选择和过滤面板?【英文标题】:Howcanthedisablefileoperation,fileselectionandfilterpanelfromJFileChooser(JAVASwing)?【发布时间】:2009-04-2809:11:49【问题描述】:我需要在Panel中嵌入JFileChooser对话... 查看详情
JFileChooser 作为 JDialog 父级
】JFileChooser作为JDialog父级【英文标题】:JFileChooserasaJDialogparent【发布时间】:2013-02-0920:55:39【问题描述】:在JFileChooser中,当尝试覆盖文件时,我希望我的程序提示用户他将要这样做。这是在用户按下JFileChooser中的APPROVE_OPTION... 查看详情
java示例代码_将文本文件保存在JFileChooser给定的路径中
java示例代码_将文本文件保存在JFileChooser给定的路径中 查看详情
为 JFileChooser 上的选定文件设置自己的文件图标
】为JFileChooser上的选定文件设置自己的文件图标【英文标题】:Setownfile-iconsforselectedfilesonJFileChooser【发布时间】:2012-05-2617:47:34【问题描述】:我有以下问题。我有一个选定的文件列表,我在JFileChooser的帮助下添加了文件。现... 查看详情
JFileChooser 无法设置默认选择
】JFileChooser无法设置默认选择【英文标题】:JFileChoosercan\'tsetdefaultselection【发布时间】:2015-06-3020:34:00【问题描述】:我想提示用户一个目录来保存一些文件。因此,我按照这个简单的演示应用程序设置了一个JFileChooser:importja... 查看详情
如何在 JFileChooser 保存对话框中处理问号或星号(“?”或“*”)?
】如何在JFileChooser保存对话框中处理问号或星号(“?”或“*”)?【英文标题】:HowdoIhandlequestionmarksorasterisks(\'?\'or\'*\')inJFileChoosersavedialog?【发布时间】:2021-06-0722:41:59【问题描述】:首先我创建一个JFileChooser,然后调用showS... 查看详情
用jfilechooser选择多个多文件,然后要显示这些文件的路径在jtextarea里。这段代码是啥?
...ile[]进行遍历getAbsolutePath这样得到文件的全路径参考技术AJFileChooserfc=newJFileChooser();JTextAreatextArea=newJTextArea();……File[]files=fc.getSelectedFiles();for(Filefile:files)textArea.append(file.getAbsolutePath());本回答被提问者采纳 查看详情
如何通过 JFileChooser 将 Icon 对象保存到文件中?
】如何通过JFileChooser将Icon对象保存到文件中?【英文标题】:HowtosaveIconobjectintoafileviaJFileChooser?【发布时间】:2011-12-0500:56:35【问题描述】:我有一个JLabel,其中保存了我的ImageIcon,如下所示:ImageIconimageIcon=sample.map();//amapmethodc... 查看详情
java示例代码_只浏览带有扩展名的文件。在swing中使用JFileChooser的xls
java示例代码_只浏览带有扩展名的文件。在swing中使用JFileChooser的xls 查看详情
java示例代码_<;移动选定的jpeg图像文件>;在JFileChooser的帮助下,在JavaSwing中选择一个文件夹
java示例代码_<;移动选定的jpeg图像文件>;在JFileChooser的帮助下,在JavaSwing中选择一个文件夹 查看详情
如何使用 JFileChooser 保存 file.txt?
】如何使用JFileChooser保存file.txt?【英文标题】:Howtosavefile.txtwithJFileChooser?【发布时间】:2014-10-0823:16:25【问题描述】:我正在开发记事本项目,想知道如何保存文件.txt,我的问题是,我保持文件打开JFileChooser,在选择了要保... 查看详情
2K 显示器上的 JFileChooser 图标
】2K显示器上的JFileChooser图标【英文标题】:JFileChoosericonson2KDisplays【发布时间】:2015-06-2921:07:33【问题描述】:知道如何制作JavaSwing文件选择器在2K显示器上看起来更好字体缩放比例>125%?我使用的是普通代码如:JFileChooserfc=n... 查看详情