我想自定义FileChooser窗口。在这里,我想插入一个标签和文本框,我也坚持如何从新添加的文本框中插入值。 在这里我做了什么。
我的CustomFileChooser:
我需要赞:
我只是扩展JFileChooser并使用下面提到的代码来添加组件:
JPanel panel = new JPanel();
JLabel lable = new JLabel("Document Name: ");
lable.setForeground(Color.RED);
docText = new JTextField();
docText.setName("documentNameText");
docText.setHorizontalAlignment(SwingConstants.LEFT);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(gl_panel
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(
gl_panel.createParallelGroup(
Alignment.LEADING)
.addComponent(
docText,
GroupLayout.PREFERRED_SIZE,
150,
GroupLayout.PREFERRED_SIZE)
.addComponent(lable))
.addContainerGap(0, Short.MAX_VALUE)));
gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
Alignment.TRAILING).addGroup(
gl_panel.createSequentialGroup()
.addContainerGap(172, Short.MAX_VALUE)
.addComponent(lable)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(docText, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE).addContainerGap()));
gl_panel.setAutoCreateGaps(true);
gl_panel.setAutoCreateContainerGaps(true);
panel.setLayout(gl_panel);
setAccessory(panel);
答案 0 :(得分:0)
我无法通过您的GroupLayout
获得结果。
但是运行并检查我创建的以下程序。我会根据您的需要为您提供Custom JFileChooser
public class CustomJFileChooser {
public static void main(String[] args) {
final JFileChooser chooser = new JFileChooser();
JComponent panel = new JPanel((LayoutManager) new FlowLayout(
FlowLayout.LEFT));
JTextField docText = new JTextField(20);
docText.setName("documentNameText");
docText.setHorizontalAlignment(SwingConstants.LEFT);
panel.add(new JLabel("Document Name: "));
panel.add(docText);
chooser.setAccessory(panel);
// This part is important
JComponent center = null;
BorderLayout layout = (BorderLayout) chooser.getLayout();
for (Component child : chooser.getComponents()) {
if (BorderLayout.CENTER == layout.getConstraints(child)) {
center = (JComponent) child;
}
}
if (center != null)
center.add(panel, BorderLayout.SOUTH);
// --------
final JFrame frame = new JFrame();
JButton button = new JButton("Open File Chooser");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(frame);
}
});
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
答案 1 :(得分:0)
我刚刚测试了另一个答案,我无法使其正常工作。我是这样做的:
JFileChooser fc = new JFileChooser();
//get the center component
BorderLayout layout = (BorderLayout) fc.getLayout();
JComponent comp = (JComponent) layout.getLayoutComponent(BorderLayout.CENTER);
layout = (BorderLayout) comp.getLayout();
comp = (JComponent) layout.getLayoutComponent(BorderLayout.CENTER);
//add my panel at the bottom
comp.add(panel, BorderLayout.SOUTH);