如何在不使用JFileChooser的情况下打开Java Swing中的文件 [英] How to open files in Java Swing without JFileChooser
问题描述
我不喜欢 JFileChooser
,因为它打开了一个浏览目录文件的小窗口。我可以在Java Swing下使用其他的而不是 JFileChooser
吗?
我试过使用SWT的元素,但是它没有工作,意思是使用按钮对象,然后在 JFrame
,但是失败了,所以我想SWT和Swing不能混合在一起?
下面是开始。将其更改为需要:
..所以我想SWT和Swing不会混在一起?
在同一个顶级容器中混合使用Swing / AWT / SWT组件通常不是一个好主意。 STRONG>。在基于Swing的 JFrame
上打开AWT FileDialog
不是问题,因为它们都是顶级容器。我很肯定,这同样适用于Swing / SWT或AWT / SWT。
I'm using Java Swing (GUI) and I want to add a button to my project for opening files.
I don't like the JFileChooser
since it opens a small window for browsing through the files of the directories. Can I use something else instead of the JFileChooser
under Java Swing?
I've tried to use elements of SWT but it didn't work, meaning is the use of the button object and then use it inside the JFrame
, but that failed, so I guess SWT and Swing don't mix together?
Here is the example of Java Swing with JFileChooser and I'm looking for something like this to put in my JFrame
.
JFileChooser
with the native PLAF seems to fulfill the stated requirement.
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class NativeFileChooser {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
}
});
}
}
Still not quite to your liking? Then you might start with this one & change it to need:
..so I guess SWT and Swing don't mix together?
It is generally not a good idea to mix Swing/AWT/SWT components in the same top-level container. It is not a problem to open an AWT FileDialog
over a Swing based JFrame
since they are both top-level containers. I am pretty sure the same would apply to Swing/SWT or AWT/SWT.
这篇关于如何在不使用JFileChooser的情况下打开Java Swing中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!