Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6928067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:08:09+00:00 2026-05-27T11:08:09+00:00

Unix / Linux support auto-complete of files and directories when pressing tab. I need

  • 0

Unix / Linux support auto-complete of files and directories when pressing “tab”.
I need to create this ability in my windows application. I have a text field for user input of file name, which I want to respond to a “tab” press like it will do when we’re in a unix console:

  1. If there is one option – Auto-complete.
  2. Some options – show a list of the options.
  3. No options – nada.

For my SSH connection to my unix machine I use the ch.ethz.ssh API.

Is there a way to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:08:09+00:00Added an answer on May 27, 2026 at 11:08 am

    First you want to have a text field without focus cycling, and tab suppression:

    jTextField1.setFocusCycleRoot(true);
    jTextField1.setFocusTraversalKeysEnabled(false);       
    

    Then a data model for the files (here local directory, but SSH is likewise):

    private File dir = new File("C:/Work");
    private String typedPrefix = null;
    private List<String> filesWithPrefix = new ArrayList<>();
    

    Then a key pressed handling for the TAB:

    • Consume the event.
    • Get the prefix upto the caret for searching file names.
    • If you merely need to restrict already found file names, so do, otherwise physical search them.
    • Look for the longest common prefix in the file names. Display that.

      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);
          }
      }
      }
      

    What is missing is the display of the ambiguous file names. Popup menu would be nice, wouldn’t it?

    Popup:

                        // 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making my PHP Command line application support Linux and Windows. Currently it has this
Is there an equivalent command on windows for the UNIX/Linux sync command? I need
What are the standard installation directories for both Windows and Unix (Linux and Mac).
If, under UNIX/Linux/BSD/OSX, I use this sequence of APIs in Application A: msgq_id =
I want to be able to do the Windows equivalent of this Unix/Linux command:
I need to run a standalone ruby script as Unix (linux) daemon. After running
Do we have similar windows API of DebugBreak in Unix/ Linux. I want to
This questions concerns mostly Unix/Linux style C++ development. I see that many C++ libraries
This question is for experienced Unix/Linux developers. If you have found that you like
I stumbled upon this website where Metatrader 4 installed under Unix/Linux based VPS. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.