Unix / Linux支持当按下“tab”时自动完成文件和目录。 我需要在我的Windows应用程序中创build这个function。 我有一个用户input文件名的文本字段,我想要响应一个“标签”按就好了,当我们在一个Unix控制台:
一些选项 – 显示选项列表。
没有select – 纳达。
对于我的SSH连接到我的Unix机器,我使用ch.ethz.ssh API。
有没有办法做到这一点?
Pyreadline在Windows上的任意自动完成
Bash Tab完成文件名后参数
如何在单独的进程中运行一个shell并获得自动完成? (python)
java linuxterminal-cp自动完成
git:哈希自动完成
如何覆盖现有的zsh键盘完成?
YouCompleteMe无法自动完成
Autohotkey中的多个游标
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。