如何用鼠标右键单击在交互式 Jtable 中打开文件选择器
How to open file chooser in the interactive Jtable with right mouse click
我正在使用基于代码 here 的交互式 JTable。
我需要的是双击编辑单元格后,
如果单击鼠标右键,它会打开文件选择器 select 文件
否则只需双击第一列中的所有单元格后手动输入路径。
我加了
TableColumn waweletFileColumn = table.getColumnModel().getColumn(InteractiveTableModel.TITLE_INDEX );
waweletFileColumn.setCellEditor(new FileChooserCellEditor());
在交互式 table 中修改单元格的行为。
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?
摆脱自定义单元格编辑器。
相反,您只使用默认编辑器,但您需要将 MouseListener 添加到编辑器的文本字段以处理右键单击并显示 JFileChooser。
所以基本逻辑可能是这样的:
JTextField editField = new JTextField()
editfield.addMouseListener(...);
DefaultCellEditor editor = new DefaultCellEditor( editField );
table.getColumnModel().getColumn(???).setCellEditor(editor);
然后将您的逻辑添加到 MouseListener
以显示 JFileChooser
。当文件选择器关闭时,您将获得选定的文件并更新文本字段。类似于:
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() );
}
我正在使用基于代码 here 的交互式 JTable。
我需要的是双击编辑单元格后,
如果单击鼠标右键,它会打开文件选择器 select 文件
否则只需双击第一列中的所有单元格后手动输入路径。
我加了
TableColumn waweletFileColumn = table.getColumnModel().getColumn(InteractiveTableModel.TITLE_INDEX );
waweletFileColumn.setCellEditor(new FileChooserCellEditor());
在交互式 table 中修改单元格的行为。
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?
摆脱自定义单元格编辑器。
相反,您只使用默认编辑器,但您需要将 MouseListener 添加到编辑器的文本字段以处理右键单击并显示 JFileChooser。
所以基本逻辑可能是这样的:
JTextField editField = new JTextField()
editfield.addMouseListener(...);
DefaultCellEditor editor = new DefaultCellEditor( editField );
table.getColumnModel().getColumn(???).setCellEditor(editor);
然后将您的逻辑添加到 MouseListener
以显示 JFileChooser
。当文件选择器关闭时,您将获得选定的文件并更新文本字段。类似于:
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() );
}