简体   繁体   English

JFileChooser 和额外的 JCheckBox 用于设置文件打开方式

[英]JFileChooser with additional JCheckBox for setting the file opening method

I am using JFileChooser as a component for selecting files in this code .我使用JFileChooser作为在此代码中选择文件的组件。

I need one additional input from the user to trigger the way how the file shall be opened.我需要用户的额外输入来触发文件的打开方式。 In my use case if it shall be read to the RAM entirely or not.在我的用例中,是否应将其完全读取到 RAM 中。

I know I can ask user elsewhere but the best would be if I can add JCheckBox to the JFileChooser dialog.我知道我可以在其他地方询问用户,但最好是如果我可以将JCheckBox添加到JFileChooser对话框中。 I want to achieve something as on the picture.我想实现如图所示的目标。

在此处输入图像描述

How I can do that and how I read the status of user input?我该如何做到这一点以及如何读取用户输入的状态?

I figured out the simplest is to utilize the mechanism that is intended for image thumbnails of selected files.我发现最简单的方法是利用用于选定文件的图像缩略图的机制。 By providing so called Accessory Component, which must be a child class of JComponent , through calling JFileChooser.setAccessory you can obtain a space to the right of file selecting rectangle.通过提供所谓的附件组件,它必须是JComponent的子 class ,通过调用JFileChooser.setAccessory可以获得文件选择矩形右侧的空间。

Including minimal example:包括最小的例子:

JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Open DEN file...");
fc.setAccessory(new CheckBoxAccessory());
int returnVal = fc.showOpenDialog(frame);
CheckBoxAccessory cba = (CheckBoxAccessory)fc.getAccessory();
boolean useVirtualStack = cba.isBoxSelected();
JOptionPane.showMessageDialog(null, String.format("selected=%b", useVirtualStack));

where the class CheckBoxAccessory looks as follows其中 class CheckBoxAccessory 如下所示

public class CheckBoxAccessory extends JComponent {
    JCheckBox virtualCheckBox;
    boolean checkBoxInit = false;
    
    int preferredWidth = 150;
    int preferredHeight = 100;//Mostly ignored as it is  
    int checkBoxPosX = 5;
    int checkBoxPosY = 20;
    int checkBoxWidth = preferredWidth;
    int checkBoxHeight = 20;
    
    public CheckBoxAccessory()
    {
        setPreferredSize(new Dimension(preferredWidth, preferredHeight));
        virtualCheckBox = new JCheckBox("Virtual stack", checkBoxInit);
        virtualCheckBox.setBounds(checkBoxPosX, checkBoxPosY, checkBoxWidth, checkBoxHeight);
        this.add(virtualCheckBox);
    }
    
    public boolean isBoxSelected()
    {
        return virtualCheckBox.isSelected();
    }
}

The result looks as follows结果如下所示在此处输入图像描述

Disadvantage is that you will not get the whole component to play with but just a relatively small box.缺点是你不会得到整个组件,而只是一个相对较小的盒子。 Thus the visual look is not what I initially wanted, but when you are no Picasso, you won't care.因此视觉效果不是我最初想要的,但当你不是毕加索时,你不会在意。 Advantage of this solution is that you can even react on change of selected file, which is in more details described in https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory此解决方案的优点是您甚至可以对所选文件的更改做出反应,在https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#accessory中有更详细的描述

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

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