【发布时间】:2014-09-22 14:33:17
【问题描述】:
我正在尝试创建一个新组件以进入 JTable。它由一个 JTextfield 组成,单击该 JTextfield 时会生成一个 JFileChooser,因此用户可以浏览文件系统并选择所需的文件,然后使用该文件的路径填充该字段。到目前为止,我已经到了可以在编辑器中填写文本字段的地步,但是当我单击它时,FilecChooser 不会产生。有人知道我在这里做错了什么吗?
public class FileBrowserCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final long serialVersionUID = 1L;
private Component frame;
JButton button;
JTextField textField;
String path;
JFileChooser fc;
protected static final String EDIT = "edit";
public FileBrowserCellEditor(Component frame){
this.frame = frame;
textField = new JTextField();
textField.setActionCommand(EDIT);
textField.addActionListener(this);
fc = new JFileChooser();
}
@Override
public Object getCellEditorValue() {
return path;
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object arg1,
boolean arg2, int arg3, int arg4) {
return textField;
}
@Override
public void actionPerformed(ActionEvent e) {
//Debug
System.out.println(e.getActionCommand());
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
textField.setText(path);
fc.showOpenDialog(frame);
fireEditingStopped(); //Make the renderer reappear.
} else { //User pressed dialog's "OK" button.
//currentColor = colorChooser.getColor();
File file = fc.getSelectedFile();
this.path = file.getAbsolutePath();
}
}
}
【问题讨论】:
-
尝试使用
MouseListener而不是ActionListener。
标签: java swing jtable jfilechooser tablecelleditor