简体   繁体   English

如何用鼠标右键单击在交互式Jtable中打开文件选择器

[英]How to open file chooser in the interactive Jtable with right mouse click

I am using interactive JTable based on the code here . 我正在根据此处的代码使用交互式JTable。

What I need is that after double-click to edit the cell, 我需要的是双击编辑单元格后,
If right mouse click, it opens file chooser to select the file 如果单击鼠标右键,它将打开文件选择器以选择文件
else just enter the path manually after double clicking for all cells in the first column. 否则,只需双击第一列中的所有单元格,然后手动输入路径。

I added 我加了

TableColumn waweletFileColumn = table.getColumnModel().getColumn(InteractiveTableModel.TITLE_INDEX );
    waweletFileColumn.setCellEditor(new FileChooserCellEditor());

in the interactive table to modify the cells' behavior. 在交互式表格中修改单元格的行为。

    public class FileChooserCellEditor extends DefaultCellEditor implements TableCellEditor {

    /** Editor component */
    private JTextField tf;
    /** Selected file */
    private String file = "";

    private String type;

    /**
     * Constructor.
     */
    public FileChooserCellEditor(String type) {
        super(new JTextField());
        this.type = type;
        // Using a JButton as the editor component
        tf = new JTextField();
        tf.setBorder(null);
    }

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

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

    file = myFileChooser(type);
    fireEditingStopped();

    tf.setText(file);
    return tf;
}

public static String myFileChooser() {

        JFileChooser chooser = new JFileChooser();

        chooser.setCurrentDirectory( new File(System.getProperty("user.home"));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
        chooser.setDialogTitle("Choose" );

        chooser.setAcceptAllFileFilterUsed(true);

        chooser.setDialogType(JFileChooser.OPEN_DIALOG);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          direct = chooser.getSelectedFile();
          return chooser.getSelectedFile().toString();
        }

        return "";

        }
}

But how can I modify the code to open the file chooser if the right mouse click is clicked and act like a normal text field otherwise? 但是,如果单击了右键单击,否则如何修改代码以打开文件选择器呢?

But how can I modify the code to open the file chooser if the right mouse click is clicked and act like a normal text field otherwise? 但是,如果单击了右键单击,否则如何修改代码以打开文件选择器呢?

Get rid of the custom cell editor. 摆脱自定义单元格编辑器。

Instead you just use the default editor but you need to add a MouseListener to the text field of the editor to handle the right click and show the JFileChooser. 取而代之的是,您仅使用默认的编辑器,但需要在编辑器的文本字段中添加MouseListener,以处理右键单击并显示JFileChooser。

So the basic logic might be something like: 因此,基本逻辑可能类似于:

JTextField editField = new JTextField()
editfield.addMouseListener(...);
DefaultCellEditor editor = new DefaultCellEditor( editField );
table.getColumnModel().getColumn(???).setCellEditor(editor);

Then add your logic to the MouseListener to display the JFileChooser . 然后将逻辑添加到MouseListener以显示JFileChooser When the file chooser is closed you get the selected file and update the text field. 关闭文件选择器后,您将获得所选文件并更新文本字段。 Something like: 就像是:

JTextField textField = (JTextField)e.getSource();
JFileChooser fc = new JFileChooser(...);
int returnVal = fc.showOpenDialog(textField);

if (returnVal == JFileChooser.APPROVE_OPTION) 
{
        File file = fc.getSelectedFile(); 
        textField.setText( file.toString() );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM