java Swing常用的知识点
1、触发文件夹选择器
private String getIndexChangedDirPath() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
String absolutePath = selectedFile.getAbsolutePath();
return absolutePath;
}
return null;
}
2、jtable增加表格双击事件
private void tableBindMouseClickEvent(JTable table) {
MouseListener mouseListener =
new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
// 获取选中的行和列
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
// 获取选中的单元格的内容
if (row >= 0 && col >= 0) {
Object cellValue = table.getValueAt(row, col);
String filePath = cellValue.toString();
if (!FileUtil.exist(filePath)) {
return;
}
// 选中文件所在的目录并且高亮显示文件
try {
Runtime.getRuntime()
.exec("explorer.exe /e, /select, " + filePath);
} catch (IOException ex) {
log.error("open file error", ex);
}
}
}
}
};
// 添加单击事件监听器
table.addMouseListener(mouseListener);
}
3、系统文件浏览器打开指定的文件并选中
Runtime.getRuntime().exec("explorer.exe /e, /select, " + filePath);
正文到此结束