自定义 javax.swing.JFileChooser 以包含附加的 JTextField

发布于 2024-08-19 16:01:08 字数 205 浏览 4 评论 0原文

我想在 FileChooser 中包含一个额外的(可选)JTextField,允许用户在选择文件时填写它,而不是在做出选择后给他们一个额外的提示。有人尝试过类似的事情并找到了可行的解决方案吗?

我的目标结果如下所示:

http://imgur.com/lVMd6

I want to include an additional (optional) JTextField in the FileChooser, allowing the user to fill it in while choosing the file rather than giving them an additional prompt after they make their choice. Has anybody attempted something similar and found a working solution?

My target result would look something like this:

http://imgur.com/lVMd6

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

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

发布评论

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

评论(2

じее 2024-08-26 16:01:08

记录将控件添加到 < code>JFileChooser 是通过 setAccessory(JComponent) 方法。

JTextField field = new JTextField("Hello, World");
JPanel accessory = new JPanel();
accessory.setLayout(new FlowLayout());
accessory.add(field);

JFileChooser chooser = new JFileChooser();
chooser.setAccessory(accessory);
int ret = chooser.showOpenDialog(frame);

但是,这会将新控件布局在对话框的右侧(确切的位置可能取决于区域设置)。

要将组件定位到您想要的位置,您可能必须遍历组件图并对其进行操作。这将是一种非常脆弱的方法,您最好构建自己的对话。

这可以包含一个文件选择器:

JFileChooser chooser = new JFileChooser();
chooser.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // TODO - wire into something
    System.out.println(e);
  }
});

JTextField field = new JTextField("Hello, World");

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(chooser, BorderLayout.CENTER);
panel.add(field, BorderLayout.SOUTH);

The documented way to add controls to a JFileChooser is via the setAccessory(JComponent) method.

JTextField field = new JTextField("Hello, World");
JPanel accessory = new JPanel();
accessory.setLayout(new FlowLayout());
accessory.add(field);

JFileChooser chooser = new JFileChooser();
chooser.setAccessory(accessory);
int ret = chooser.showOpenDialog(frame);

However, this will layout the new control on the right of the dialog (exact positioning is probably locale-dependent).

To locate the component to the position you want it, you'll probably have to walk the component graph and manipulate it. This would be a very fragile approach and you may be better off just building your own dialog.

This could incorporate a file chooser:

JFileChooser chooser = new JFileChooser();
chooser.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // TODO - wire into something
    System.out.println(e);
  }
});

JTextField field = new JTextField("Hello, World");

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(chooser, BorderLayout.CENTER);
panel.add(field, BorderLayout.SOUTH);
套路撩心 2024-08-26 16:01:08

多年以后...

我可以使用以下方法轻松实现此目的:

  1. 获取文件选择器的布局管理器(这是一个 BorderLayout)。

  2. 使用布局管理器,从文件选择器中获取 SOUTH 组件,保存其引用并将其从文件选择器中删除。

  3. 创建一个新面板,其中包含您想要的任何内容以及原始的 SOUTH 组件。我个人使用垂直的 BoxLayout。我还在组件之间添加了 (1,12) 的 Box.Filler。

  4. 将新面板设置为文件选择的 SOUTH 组件。

  5. 瞧!

    JFileChooser 选择器 = new JFileChooser();
    
    BorderLayout ChooserLayout = (BorderLayout) Chooser.getLayout();
    JPanel ChooserSouthComponent = (JPanel) ChooserLayout.getLayoutComponent( Chooser, BorderLayout.SOUTH );
    选择器. 删除( 选择器SouthComponent );
    
    JPanel newSouthComponent = new JPanel();
    newSouthComponent.setLayout( new BoxLayout( newSouthComponent, BoxLayout.PAGE_AXIS ) );
    
    JPanel 实用程序 = new JPanel();
    JTextField 字段 = new JTextField("你好,世界");
    TitledBorder 标题 = BorderFactory.createTitledBorder( "标题" );
    实用程序.setBorder(标题);
    实用程序.add( 字段 );
    
    维度填充维度 = new Dimension( 1, 12 );
    
    newSouthComponent.add( 实用程序 );
    newSouthComponent.add( new Box.Filler( FillerDimension, FillerDimension, FillerDimension ) );
    newSouthComponent.add( 选择器SouthComponent );
    
    selecter.add( newSouthComponent, BorderLayout.SOUTH );
    
    选择器.showOpenDialog( null );
    

Years and years later...

I could archieve this quite easily using this method:

  1. Get the layout manager of the file chooser (which is a BorderLayout).

  2. Using the layout manager, get the SOUTH component from the file chooser, save its reference and remove it from the file chooser.

  3. create a new panel containing whatever you want as well as the original SOUTH component. I personally use a vertical BoxLayout. I also add a Box.Filler of (1,12) between the components.

  4. Set the new panel as the SOUTH component of the file choose.

  5. Voilà !

    JFileChooser chooser = new JFileChooser();
    
    BorderLayout chooserLayout = (BorderLayout) chooser.getLayout();
    JPanel chooserSouthComponent = (JPanel) chooserLayout.getLayoutComponent( chooser, BorderLayout.SOUTH );
    chooser.remove( chooserSouthComponent );
    
    JPanel newSouthComponent = new JPanel();
    newSouthComponent.setLayout( new BoxLayout( newSouthComponent, BoxLayout.PAGE_AXIS ) );
    
    JPanel utility = new JPanel();
    JTextField field = new JTextField("Hello, World");
    TitledBorder title = BorderFactory.createTitledBorder( "title" );
    utility.setBorder(title);
    utility.add( field );
    
    Dimension fillerDimension = new Dimension( 1, 12 );
    
    newSouthComponent.add( utility );
    newSouthComponent.add( new Box.Filler( fillerDimension, fillerDimension, fillerDimension ) );
    newSouthComponent.add( chooserSouthComponent );
    
    chooser.add( newSouthComponent, BorderLayout.SOUTH );
    
    chooser.showOpenDialog( null );
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文