[英]Windows application with Auto-complete using tab of unix machine files and directories
当按下“tab”时,Unix / Linux支持自动完成文件和目录。 我需要在我的Windows应用程序中创建此功能。 我有一个用于文件名用户输入的文本字段,我想响应“tab”按下,就像我们在unix控制台中一样:
对于我与我的unix机器的SSH连接,我使用ch.ethz.ssh API。
有办法吗?
首先,您希望拥有一个没有焦点循环和制表符抑制的文本字段:
jTextField1.setFocusCycleRoot(true);
jTextField1.setFocusTraversalKeysEnabled(false);
然后是文件的数据模型(这里是本地目录,但SSH同样如此):
private File dir = new File("C:/Work");
private String typedPrefix = null;
private List<String> filesWithPrefix = new ArrayList<>();
然后按下按键处理TAB:
在文件名中查找最长的公共前缀。 显示出来。
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) { System.out.println("KeyPressed " + evt); if (evt.getKeyCode() == KeyEvent.VK_TAB) { evt.consume(); int caretPos = jTextField1.getCaretPosition(); try { final String newPrefix = jTextField1.getText(0, caretPos); System.out.println("newPrefix: " + newPrefix); if (!newPrefix.isEmpty()) { if (typedPrefix == null || !newPrefix.startsWith(typedPrefix)) { // Must physically reload possible values: String[] fileNames = dir.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(newPrefix); } }); filesWithPrefix.clear(); Collections.addAll(filesWithPrefix, fileNames); typedPrefix = newPrefix; } else { // Can reduce prior selection: for (ListIterator<String> it = filesWithPrefix.listIterator(); it.hasNext(); ) { String fileName = it.next(); if (!fileName.startsWith(newPrefix)) { it.remove(); } } typedPrefix = newPrefix; } System.out.println("filesWithPrefix: " +filesWithPrefix); if (!filesWithPrefix.isEmpty()) { // Find longest common prefix: String longestCommonPrefix = null; for (String fileName : filesWithPrefix) { if (longestCommonPrefix == null) { longestCommonPrefix = fileName; } else { while (!fileName.startsWith(longestCommonPrefix)) { longestCommonPrefix = longestCommonPrefix.substring(0, longestCommonPrefix.length() - 1); } } } if (longestCommonPrefix.length() > typedPrefix.length()) { jTextField1.setText(longestCommonPrefix); jTextField1.setCaretPosition(longestCommonPrefix.length()); typedPrefix = longestCommonPrefix; } if (filesWithPrefix.size() > 1) { // Show popup: ;;; } else if (filesWithPrefix.size() == 1) { // File selected: System.beep(); } } } } catch (BadLocationException ex) { Logger.getLogger(TabsJFrame.class.getName()).log(Level.SEVERE, null, ex); } } }
缺少的是显示不明确的文件名。 弹出菜单会很好,不是吗?
弹出:
// Show popup:
JPopupMenu popup = new JPopupMenu();
for (String fileName : filesWithPrefix) {
popup.add(new AbstractAction(fileName) {
@Override
public void actionPerformed(ActionEvent e) {
jTextField1.setText(e.getActionCommand());
}
});
}
Point pt = jTextField1.getCaret().getMagicCaretPosition();
popup.show(jTextField1, pt.x, pt.y + 5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.