简体   繁体   English

可以从 JFileChooser 中删除标题栏吗?

[英]Possible to remove Title bar from JFileChooser?

I am trying to display a simple JFileChooser that has no Titlebar.我正在尝试显示一个没有标题栏的简单 JFileChooser。 Below is the example code:下面是示例代码:

package ca.customfilepicker.main;
import java.awt.Component;
import java.awt.HeadlessException;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

class CustomFileChooser

{

    public static void main(String args[]) {

        JFileChooser chooser = new JFileChooser() {
            @Override
            protected JDialog createDialog(Component parent) throws HeadlessException {
            
                JDialog diag = super.createDialog(parent);
                
                //diag.setUndecorated(true);
                return diag;
            }
        };
        
        chooser.setBorder(BorderFactory.createTitledBorder("Open"));
        chooser.showOpenDialog(null);

    }
}

So essentially I want the Border I set to be the top level Title bar.所以基本上我希望我设置的边框是顶级标题栏。 Example image:示例图像:

https://i.stack.imgur.com/exogU.png

So far I have had zero luck achieving this, nor found any others looking for a similar appearance.到目前为止,我实现这一目标的运气为零,也没有发现任何其他人在寻找类似的外观。 Appreciate the help!感谢帮助! Cheers干杯

The JFileChooser is just a Swing component. JFileChooser只是一个 Swing 组件。 It can be added to any Container.它可以添加到任何容器中。

So you could create an undecorated JDialog and add an instance of the JFileChooser to the dialog.因此,您可以创建一个未修饰的 JDialog 并将 JFileChooser 的一个实例添加到对话框中。

The problem is now that the "Open" and "Cancel" buttons won't close the dialog, so you would need to to that manually.现在的问题是“打开”和“取消”按钮不会关闭对话框,因此您需要手动关闭。 You could probably override the "approveSelection() and cancelSelection()` methods of the JFileChooser.您可能会覆盖 JFileChooser 的“approveSelection() and cancelSelection()”方法。

I would guess the logic would be to invoke super.approveSelection() or super.cancelSelection() and then use the SwingUtilities.windowForComponent(...) method to get the parent window and then invoke dispose() on the window.我猜逻辑是调用 super.approveSelection() 或 super.cancelSelection(),然后使用SwingUtilities.windowForComponent(...)方法获取父 window,然后在 window 上调用dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM