如何在其他窗口上生成一个独立的JFileChooser对话框? [英] How to generate a stand-alone JFileChooser dialog box on top of other windows?
问题描述
我知道这些类似的问题:
$ b从所有窗口中弹出的JFileChooser / 7494544#7494544 $ b
...但这些的答案问题似乎过于复杂,涉及到创建更多的GUI元素,我不敢相信将需要。
我也意识到建议这里关于不混合控制台和Swing接口,但我想保持尽可能简单。
我想知道如何生成一个JFileChooser(showOpenDialog)对话框,而不是 >创建其他GUI元素(JPanel等)。
注意1:本网站似乎在讨论一个解决方案,但很难遵循。 我的代码,它现在创建一个隐藏的对话框,在这里: 所评论的代码是我所尝试过的所有不起作用的东西,包括 不能说如果这可能有帮助,因为我不能重现问题(Windows Vista中,jdk7) - 无论我尝试,对话框出现在一切,可能高度依赖于操作系统。 Like some other people who have asked similar questions, I was going nuts trying to 'fix' my JFileChooser dialog box generation code until I noticed that it is being generated, but it is appearing underneath all other windows and does not have an associated taskbar icon (so there was no clue at all that it existed!). I am aware of these similar questions: ...but the answers to those questions seem overly complex, involving creating more GUI elements, which I can't believe would be required. I am also aware of the advice here about not mixing console and Swing interfaces, but I want to keep things as simple as possible. I would like to know how to generate a JFileChooser (showOpenDialog) dialog box that is above other windows without resorting to creating other GUI elements (JPanel etc.). Note 1: This site seems to discuss a solution, but is hard to follow. My code, which right now creates a buried dialog box, is here: The commented code is all of the things I have tried that have not worked, including
注2:如果我所要求的是不可能的,那么关于如何至少给对话框一个任务栏图标的信息(再次不需要有一个父母)将是很好的。
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
class Client {
String currentDirectoryFolderPath =H:\\myFolder;
javax.swing.JFileChooser jFileChooser =
new JFileChooser(currentDirectoryFolderPath);
jFileChooser.setVisible(true); //默认为不可见?!?
javax.swing.filechooser.FileNameExtensionFilter fileExtensionFilter
= new FileNameExtensionFilter(
逗号分隔值和文本文件),
csv,txt);
jFileChooser.setFileFilter(fileExtensionFilter);
// int returnVal = jFileChooser.showOpenDialog(jFileChooser);
//jFileChooser.showDialog(null,testing 1--2--3) ;
//jFileChooser.requestFocusInWindow();
//jFileChooser.requestFocus();
//jFileChooser.showOpenDialog(null);
//jFileChooser.requestFocus();
int returnVal = jFileChooser.showOpenDialog(null);
$ b $ if(returnVal == JFileChooser.APPROVE_OPTION){
System.out.println(您选择打开这个文件: +
jFileChooser.getSelectedFile()。getName());
}
System.out.println(JFileChooser.APPROVE_OPTION);
System.out.println (jFileChooser);
}
$ b
public static void main(String [] args)throws AWTException {
Action action = new AbstractAction(open in tray){
@Override
public void actionPerformed(ActionEvent e){
System.out.println (请选择文件);
JFileChooser fc = new JFileChooser(){
$ b $ @覆盖
保护JDialog createDialog(组件父)
抛出HeadlessException {
JDialog dialog = super.createDialog (父);
//根据需要在这里配置 - 只是为了看到不同
dialog.setLocationByPlatform(true);
//可能会帮助 - 无法知道,因为我无法重现问题
dialog.setAlwaysOnTop(true);
返回对话框;
}
};
int retValue = fc.showOpenDialog(null);
if(retValue == JFileChooser.APPROVE_OPTION){
System.out.println(fc.getSelectedFile());
} else {
System.out.println(下一次选择文件);
}
}
};
TrayIcon trayIcon = new TrayIcon(XTestUtils.loadDefaultImage(),in tray in tray);
trayIcon.addActionListener(action);
SystemTray.getSystemTray()。add(trayIcon);
$ b
Note 2: If what I am asking for is impossible, then information about how to at least give the dialog box a taskbar icon (again without requiring it to have a parent) would be great.import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
class Client {
String currentDirectoryFolderPath = "H:\\myFolder";
javax.swing.JFileChooser jFileChooser =
new JFileChooser(currentDirectoryFolderPath);
jFileChooser.setVisible(true); //defaults to invisible?!?
javax.swing.filechooser.FileNameExtensionFilter fileExtensionFilter
= new FileNameExtensionFilter(
comma-separated values and text files",
"csv", "txt");
jFileChooser.setFileFilter(fileExtensionFilter);
//int returnVal = jFileChooser.showOpenDialog(jFileChooser);
//jFileChooser.showDialog(null, "testing 1--2--3");
//jFileChooser.requestFocusInWindow();
//jFileChooser.requestFocus();
//jFileChooser.showOpenDialog(null);
//jFileChooser.requestFocus();
int returnVal = jFileChooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
jFileChooser.getSelectedFile().getName());
}
System.out.println(JFileChooser.APPROVE_OPTION);
System.out.println(jFileChooser);
}
Another option to configure the dialog: which is to subclass JFileChooser and override its createDialog with custom settings:
public static void main(String[] args) throws AWTException {
Action action = new AbstractAction("open in tray") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Please select the file");
JFileChooser fc = new JFileChooser() {
@Override
protected JDialog createDialog(Component parent)
throws HeadlessException {
JDialog dialog = super.createDialog(parent);
// config here as needed - just to see a difference
dialog.setLocationByPlatform(true);
// might help - can't know because I can't reproduce the problem
dialog.setAlwaysOnTop(true);
return dialog;
}
};
int retValue = fc.showOpenDialog(null);
if(retValue == JFileChooser.APPROVE_OPTION){
System.out.println(fc.getSelectedFile());
}else {
System.out.println("Next time select a file.");
}
}
};
TrayIcon trayIcon = new TrayIcon(XTestUtils.loadDefaultImage(), "open in tray");
trayIcon.addActionListener(action);
SystemTray.getSystemTray().add(trayIcon);
}
Can't say if that might help because I can't reproduce the problem (Windows Vista, jdk7) - whatever I tried, the dialog appears on top of everything, probably highly OS dependent.
这篇关于如何在其他窗口上生成一个独立的JFileChooser对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!