6
我想在FileChooser中包含一個額外的(可選的)JTextField,允許用戶在選擇文件時填充它,而不是在他們創建文件後給他們一個額外的提示選擇。有沒有人嘗試類似的東西,找到了一個可行的解決方案?自定義javax.swing.JFileChooser以包含其他JTextField
我的目標結果會是這個樣子:
我想在FileChooser中包含一個額外的(可選的)JTextField,允許用戶在選擇文件時填充它,而不是在他們創建文件後給他們一個額外的提示選擇。有沒有人嘗試類似的東西,找到了一個可行的解決方案?自定義javax.swing.JFileChooser以包含其他JTextField
我的目標結果會是這個樣子:
的documented方式將控件添加到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);
+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;; :) – 2010-01-28 20:55:17