情况1:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(this); //this = parent Component
Run Code Online (Sandbox Code Playgroud)
案例2:
JFileChooser myFileChooser;
myFileChooser.showOpenDialog(null);
Run Code Online (Sandbox Code Playgroud)
这两种情况有什么实际区别?
有一段时间没有写代码了,所以我觉得我有点生疏了。我正在尝试构建一个应用程序,让用户选择一个文件作为输入。以下代码是我目前所拥有的:
JButton btnFile = new JButton("Select Excel File");
btnFile.addActionListener(new ActionListener() {
//Handle open button action.
public void actionPerformed(ActionEvent e) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(frmRenamePdfs);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("File: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user.");
}
System.out.println(returnVal);
}
});
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚如何从侦听器外部访问“文件”,即在创建 GUI 其余部分的函数中。我在启动文件选择器的按钮旁边有一个空白文本标签,所以我想要做的是存储文件,并将文本标签的文本设置为文件名。
我收到此运行时错误,我正在尝试使 Java 文件选择器看起来像 Windows 文件选择器。
错误代码:
Exception in thread "main" java.lang.NullPointerException
at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installComponents(WindowsFileChooserUI.java:306)
at javax.swing.plaf.basic.BasicFileChooserUI.installUI(BasicFileChooserUI.java:173)
at com.sun.java.swing.plaf.windows.WindowsFileChooserUI.installUI(WindowsFileChooserUI.java:150)
at Main.getImg(Main.java:49)
at Main.main(Main.java:19)
Run Code Online (Sandbox Code Playgroud)
代码:
JFileChooser fico = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(fico);
wui.installUI(fico);
int returnVal = fico.showOpenDialog(null);
Run Code Online (Sandbox Code Playgroud) 大家晚上好,我想知道我将使用什么语法来获取 JFileChooser 中所选文件的大小 (mb) 和名称。
JFileChooser filedlg = new JFileChooser();
filedlg.showOpenDialog(null);
textField.setText(//File's name);
textField_1.setText(//File's size);
Run Code Online (Sandbox Code Playgroud)
我会用什么来替换评论?感谢所有帮助。
我有一个JFileChooser,我想在类型中提供不同的选项来更改扩展名。我想要的选项是
现在我有:
JFileChooser chooser = new WritableFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());
chooser.setFileFilter(new FileFilter()
{
@Override
public boolean accept(File file)
{
if (file.isDirectory())
{
return true;
}
else if (file.isFile())
{
String lcFileName = file.getName().toLowerCase(Locale.ROOT);
return (lcFileName.endsWith(TXT_FILE_EXTENSION) || lcFileName.endsWith(HTML_FILE_EXTENSION) || lcFileName.endsWith(XML_FILE_EXTENSION) }
return false;
}
@Override
public String getDescription()
{
return Constant.messages.getString("file.format.html");
}
Run Code Online (Sandbox Code Playgroud)
但在文件类型过滤器中只有所有文件和 HTML 可用。理想情况下,我也想摆脱所有文件选项。
此外,我有两种不同格式的 .html 要生成,是否有任何指标可以添加,以便文件选择器足够聪明,可以知道我想要哪个?
我在学习JavaFx的过程中,遇到了一个问题。我尝试使用 JavaFx 中的 FileChooser,就像我习惯在 main() 方法中使用 Swing 中的 JFileChooser 一样。但是我发现我需要一个 Window 对象。我尝试寻找解决方法,但没有找到。我还尝试过 null (就像您在 JFileChooser 中所做的那样)和 new Stage(),所以这些都不在讨论之列。我试图模仿 JFileChooser.showOpenDialog()。有什么合理的方法可以让它发挥作用吗?
所以我有这个非常基本的代码片段,用文件名过滤器初始化JFileChooser.目前你应该只看到.txt文件,稍后我想使用自定义文件名.
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter(".txt",".txt"));
dialog.setVisible(true);
dialog.showOpenDialog(dialog);
Run Code Online (Sandbox Code Playgroud)
问题是,当应用过滤器时,没有显示任何文件,甚至没有.txt文件,因为它们应该是.我已尝试使用几个文件结尾,我也尝试使用"*.txt",但没有任何作用.这真的困扰我,因为我只是不明白为什么基本的东西不起作用..
这是一个奇怪的问题.我有一个解决方案,但我不知道为什么问题出现在第一位.请注意以下代码:
// VERSION 1
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
mainFrame.setVisible(true);
JButton inputFileButton = new JButton("BROWSE");
inputPanel.add(inputFileButton);
}
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作.该按钮不执行任何操作,但它可以正确呈现.现在,我添加一个JFileChooser(我计划稍后再做一些事情,但是现在我正在做的就是实例化它).
// VERSION 2
public class test {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Test");
JPanel inputPanel = new JPanel();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(BorderLayout.CENTER, inputPanel);
mainFrame.setBounds(100, 50, 200, 100);
mainFrame.setVisible(true);
JFileChooser inputFileChooser = new JFileChooser(); // NEW LINE …Run Code Online (Sandbox Code Playgroud) 我想设置一个jFileChooser作为表的单个单元格的编辑器(不是该表的整个列,因为将使用各种其他编辑器,如comboBox等).有任何建议或示例代码吗?(我已经在这里查看了这些示例如何使用Oracle的表
我刚刚将Java JFileChooser实现到我的程序中,并想知道是否有可以添加的后退按钮.在Microsoft Word或Photoshop等程序中的大多数文件选择器中,您可以遍历目录.此选项非常有用,并且想知道是否有任何方法可以执行此操作.
java ×10
jfilechooser ×10
swing ×6
button ×1
components ×1
filechooser ×1
javafx ×1
jtable ×1
parent ×1
scope ×1
stage ×1