自定义 javax.swing.JFileChooser 以包含附加的 JTextField
我想在 FileChooser 中包含一个额外的(可选)JTextField,允许用户在选择文件时填写它,而不是在做出选择后给他们一个额外的提示。有人尝试过类似的事情并找到了可行的解决方案吗?
我的目标结果如下所示:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
记录将控件添加到 < code>JFileChooser 是通过 setAccessory(JComponent) 方法。
但是,这会将新控件布局在对话框的右侧(确切的位置可能取决于区域设置)。
要将组件定位到您想要的位置,您可能必须遍历组件图并对其进行操作。这将是一种非常脆弱的方法,您最好构建自己的对话。
这可以包含一个文件选择器:
The documented way to add controls to a
JFileChooser
is via the setAccessory(JComponent) method.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:
多年以后...
我可以使用以下方法轻松实现此目的:
获取文件选择器的布局管理器(这是一个 BorderLayout)。
使用布局管理器,从文件选择器中获取 SOUTH 组件,保存其引用并将其从文件选择器中删除。
创建一个新面板,其中包含您想要的任何内容以及原始的 SOUTH 组件。我个人使用垂直的 BoxLayout。我还在组件之间添加了 (1,12) 的 Box.Filler。
将新面板设置为文件选择的 SOUTH 组件。
瞧!
Years and years later...
I could archieve this quite easily using this method:
Get the layout manager of the file chooser (which is a BorderLayout).
Using the layout manager, get the SOUTH component from the file chooser, save its reference and remove it from the file chooser.
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.
Set the new panel as the SOUTH component of the file choose.
Voilà !