我正在尝试创建一个新组件以放入JTable中。它由一个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();

    }

}

}

最佳答案

这是一个简单的单元格编辑器,双击该单元格将显示一个JFileChooser

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.File;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;

public class FileChooserCellEditor extends DefaultCellEditor implements TableCellEditor {

    /** Number of clicks to start editing */
    private static final int CLICK_COUNT_TO_START = 2;
    /** Editor component */
    private JButton button;
    /** File chooser */
    private JFileChooser fileChooser;
    /** Selected file */
    private String file = "";

    /**
     * Constructor.
     */
    public FileChooserCellEditor() {
        super(new JTextField());
        setClickCountToStart(CLICK_COUNT_TO_START);

        // Using a JButton as the editor component
        button = new JButton();
        button.setBackground(Color.white);
        button.setFont(button.getFont().deriveFont(Font.PLAIN));
        button.setBorder(null);

        // Dialog which will do the actual editing
        fileChooser = new JFileChooser();
    }

    @Override
    public Object getCellEditorValue() {
        return file;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        file = value.toString();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                fileChooser.setSelectedFile(new File(file));
                if (fileChooser.showOpenDialog(button) == JFileChooser.APPROVE_OPTION) {
                    file = fileChooser.getSelectedFile().getAbsolutePath();
                }
                fireEditingStopped();
            }
        });
        button.setText(file);
        return button;
    }
}


要将其添加到您的JTable中:

yourJTable.getColumnModel().getColumn(yourColumnIndex).setCellEditor(new FileChooserCellEditor());

08-18 20:09