嵌入 JPanel 中的 JFileChooser

发布于 2024-07-07 23:30:55 字数 1047 浏览 6 评论 0原文

我正在编写一个需要文件打开对话框的java程序。 文件打开对话框并不困难,我希望使用JFileChooser。 我的问题是我想要一个双窗格 JFrame (由 2 个 JPanels 组成)。 左侧面板将有一个 JList,右侧面板将有一个文件打开对话框。

当我使用 JFileChooser.showOpenDialog() 时,这会在所有其他窗口之上打开对话框,这不是我想要的。 有没有办法让 JFileChooser (或者可能是另一个文件选择对话框)显示在 JPanel 内部而不是在其上方弹出?

这是我尝试过的代码,此时它非常简化。 此时我只想将 JFileChooser 嵌入到 JPanel 中。

public class JFC extends JFrame{
    public JFC()
    {
        setSize(800,600);

        JPanel panel= new JPanel();

        JFileChooser chooser = new JFileChooser();
        panel.add(chooser);

        setVisible(true);

        chooser.showOpenDialog(null);
    }

    public static void main(String[] args)
    {
        JFC blah = new JFC();
    }
}

我还尝试使用 thispanel 调用 chooser.showOpenDialog,但无济于事。 另外,我尝试将 JFileChooser 直接添加到框架中。 上面列出的两种尝试仍然会在框架或面板前面弹出 JFileChooser(具体取决于我将 JFileChooser 添加到的位置)。

I am writing a java program that needs a file open dialog. The file open dialog isn't difficult, I'm hoping to use a JFileChooser. My problem is that I would like to have a dual pane JFrame (consisting of 2 JPanels). The left panel would have a JList, and the right panel would have a file open dialog.

When I use JFileChooser.showOpenDialog() this opens the dialog box above all other windows, which isn't what I want. Is there any way to have the JFileChooser (or maybe another file selection dialog) display inside a JPanel and not pop-up above it?

Here is the code that I've tried, at this point it's very simplified. I'm only trying to get the JFileChooser to be embedded in the JPanel at this point.

public class JFC extends JFrame{
    public JFC()
    {
        setSize(800,600);

        JPanel panel= new JPanel();

        JFileChooser chooser = new JFileChooser();
        panel.add(chooser);

        setVisible(true);

        chooser.showOpenDialog(null);
    }

    public static void main(String[] args)
    {
        JFC blah = new JFC();
    }
}

I have also tried calling chooser.showOpenDialog with this and panel, but to no avail. Also, I have tried adding the JFileChooser directly to the frame. Both of the attempts listed above still have the JFileChooser pop up in front of the frame or panel (depending on which I add the JFileChooser to).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

_蜘蛛 2024-07-14 23:30:55

JFileChooser 扩展了 JComponent 和 Component,因此您应该能够将其直接添加到框架中。

JFileChooser fc = ...
JPanel panel ...
panel.add(fc);

JFileChooser extends JComponent and Component so you should be able to add it directly to your frame.

JFileChooser fc = ...
JPanel panel ...
panel.add(fc);
已下线请稍等 2024-07-14 23:30:55

要访问文件选择器中的“按钮”,您必须向其中添加一个 ActionListener:

fileChooser.addActionListener(this);
[...]

public void actionPerformed(ActionEvent action)
{
    if (action.getActionCommand().equals("CancelSelection"))
    {
        System.out.printf("CancelSelection\n");
        this.setVisible(false);
        this.dispose();
    }
    if (action.getActionCommand().equals("ApproveSelection"))
    {
        System.out.printf("ApproveSelection\n");
        this.setVisible(false);
        this.dispose();
    }
}

To access the "buttons" in the file chooser, you will have to add an ActionListener to it:

fileChooser.addActionListener(this);
[...]

public void actionPerformed(ActionEvent action)
{
    if (action.getActionCommand().equals("CancelSelection"))
    {
        System.out.printf("CancelSelection\n");
        this.setVisible(false);
        this.dispose();
    }
    if (action.getActionCommand().equals("ApproveSelection"))
    {
        System.out.printf("ApproveSelection\n");
        this.setVisible(false);
        this.dispose();
    }
}

如果您要动态添加 JFileChooser,则需要调用 revalidate()。

史蒂夫的回答是正确的。 您可以将 JFileChooser 添加到其他容器。

If you are adding the JFileChooser on the fly, you will need to call revalidate().

Steve's answer is correct. You can add a JFileChooser to other containers.

孤独岁月 2024-07-14 23:30:55

致约翰内斯:感谢您提供有用的片段。

我使用定义的常量 JFileChooser.APPROVE_SELECTIONJFileChooser.CANCEL_SELECTION 而不是“ApproveSelection”和“CancelSelection”

To Johannes: thanks for your useful snippet.

Instead of "ApproveSelection" and "CancelSelection" I used the defined constants JFileChooser.APPROVE_SELECTION and JFileChooser.CANCEL_SELECTION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文