[英]JFileChooser in front of fullscreen Swing application
I know there are some topics relative to this question (mainly this unanswered one and this one which is not handling full screen app). 我知道有一些与这个问题有关的主题(主要是这个未回答的问题和这个没有处理全屏应用程序的问题)。
I basically tried every combination of first topic sample and available methods (requestFocus, requestFocusInWindow, ...) but JFileChooser is always displaying behind the fullscreen app. 我基本上尝试了第一个主题示例和可用方法(requestFocus,requestFocusInWindow等)的每种组合,但是JFileChooser始终显示在全屏应用程序的后面。 I tried to change filechooser's parent too (setting it to null, itself or the parent frame) with no more success.
我也尝试更改filechooser的父项(将其自身设置为null或将其设置为父框架),但没有成功。
Have anyone a working example of this not-that-much-particular use case? 有没有人是这个不太特殊的用例的可行示例? Or is there a workaround to let user select files in a fullscreen app?
还是有一种解决方法让用户在全屏应用程序中选择文件?
Unfortunately I can't say how you realised the implementation of the fullscreen app. 不幸的是,我不能说您是如何实现全屏应用程序的。 But I tried a few things and came up with this:
但我尝试了几件事,并提出了以下建议:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
public Gui() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
// Set some charateristics of the frame
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setBackground(Color.black);
this.setUndecorated(true);
JButton a = new JButton("PRESS ME!");
a.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(getParent());
}
});
this.add(a);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Gui();
}
});
}
}
Pay attention to the fact, that I created a new JFileChooser with the parent of the current JFrame as parameter. 请注意,我创建了一个新的JFileChooser,并将当前JFrame的父级作为参数。
EDIT: I now even tried to set 编辑:我现在甚至试图设置
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(new Gui());
and without the 而且没有
this.setUndecorated(true);
it worked for me (got a nice fullscreen view and the JFileChooser was in the front). 它对我有用(获得了不错的全屏视图,并且JFileChooser位于前面)。 I believe the problem with the window decoration is linked to my window manager (I'm using linux with gnome).
我相信窗户装饰的问题与我的窗户管理器有关(我在gnome中使用linux)。
Hopefully this solution works for you, if not: Could you explain a little bit more, how you create the fullscreen app? 希望该解决方案对您有效,如果不能解决的话:您能否进一步说明如何创建全屏应用程序?
I would suggest instead of using using a Popup, just embed the JFileChooser
into your application. 我建议不要使用Popup,而
JFileChooser
嵌入到您的应用程序中。 It doesn't really make sense to have popups in a windowless application (Personally, I don't like popups much anyways). 在无窗口的应用程序中有弹出窗口真的没有任何意义(个人而言,我还是不太喜欢弹出窗口)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FullScreenApp {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); // Ugh.
frame.setVisible(true);
final Box panel = Box.createVerticalBox();
JButton btn = new JButton();
btn.setText("Button");
panel.add(btn);
frame.add(panel);
final CustomFileChooser chooser = new CustomFileChooser(panel);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooser.show();
}
});
}
public static class CustomFileChooser extends JFileChooser{
/** Node this chooser should be added to.
* There's likely a better way of doing this,
* but it was convenient for a quick example */
Container parent;
public CustomFileChooser(Container parent){
super();
this.parent = parent;
//Make configurations for your file chooser
setApproveButtonText("Open");
}
@Override
public void approveSelection(){
super.approveSelection();
//Perform accept action here
System.out.println(getSelectedFile().getAbsolutePath());
parent.remove(CustomFileChooser.this);
parent.repaint();
}
@Override
public void cancelSelection(){
super.cancelSelection();
//Perform cancel action here
System.out.println("Canceled");
parent.remove(CustomFileChooser.this);
parent.repaint();
}
@Override
public void show(){
rescanCurrentDirectory();
parent.add(this);
revalidate();
repaint();
}
@Override
public Dimension getMaximumSize(){
//Not necessary - But I felt the chooser should have a maximum size
return new Dimension(500,300);
}
}
}
//import
import argha.util.Fullscreen;
//this for JFrame
//true for setting Undecorated on/off
Fullscreen screen = new Fullscreen(this, true);
screen.DoTheWorkFor();
You can use my library for creating fullscreen windows and the problem you are facing hope it solved after that i tested and its working. 您可以使用我的库来创建全屏窗口,并且您遇到的问题希望在我测试并正常运行后可以解决。
Hope it may helped you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.