6

Is there any good ready made Java Swing component that can be used to implement auto complete textbox?

Sudar
  • 18,954
  • 30
  • 85
  • 131

5 Answers5

4

Here's an example on autocompletion without a drop down selection. It will only autocomplete for you when a unique match is found. The completion is auto inserted into the text field and selected. Kind of similar as to how Safari's address field works.

The solution could be expanded to provide a list of options, however that was not part of my requirements when doing this.

UPDATE

I lost the domain with the original code. It can now be accessed on github: https://github.com/sasjo/autocomplete

Samuel Sjöberg
  • 719
  • 5
  • 12
1

There is a claim that JIDE-OSS has the feature that you are requesting. That is what this forum claims, however I have not tried it.

monksy
  • 14,156
  • 17
  • 75
  • 124
0

A little dated, but with a Java Web Start File attached, which actually works: http://www.jroller.com/santhosh/date/20050620.

miku
  • 181,842
  • 47
  • 306
  • 310
0

I had some good success by using a combo box, setting the layout manager of the combo box to be border layout and then sticking in an editable JTextField. The user edits the text field which you can add a document listener to and trigger the underlying combo box to show the dropdown showing current suggestions. You can listen to the combo box for item selections and update the JTextField with the selected item. You can open the combo box using:

getUI().setPopupVisible(JComboBox c, boolean v)

To prevent accidental opening of the combo box from clicking the edge ensure you give it a zero size empty border.

William Jarvis
  • 347
  • 3
  • 6
  • Are you able to provide some more code for this by way of an example? – SteveR Apr 23 '20 at 08:17
  • Afraid it got re-implemented as part of a refactor. The new design does this in principle: The editing portion uses a JTextField. This has a document listener added to it to detect the text being changed and trigger the drop down. The second portion is the dropdown part which was done by using a JPopupMenu which contains a JList inside of it to display the items. Key events are added to the JTextField to move the selected item index up and down. Selecting an item can be done via mouse events on the JList or listening for the enter key press. Both of these should replace the text with the item. – William Jarvis Jul 14 '20 at 16:37
  • (Sorry I cannot give you the code I wrote directly as I don't own it) – William Jarvis Jul 14 '20 at 16:41
  • Actually my JavaFx implementation I managed to fork with permission to an open source personal project of mine. The design is very similar to the above but in JavaFx. Have a look in the utilities pack for a utility called FilteredComboBox. https://github.com/nottud/Kronny – William Jarvis Jul 14 '20 at 17:17
0

I hated the idea of a third-party solution, so I was always looking a way far from it. I've settle with the JTextField and a hidden JComboBox solution when I was looking with the same idea. Lately, I found something new, and seems like nobody's looking at it as alternative solution. Doing like, textfield and a clean JPopUpMenu. . .

https://www.geeksforgeeks.org/java-swing-jpopupmenu/

Maybe it could help, for others at least. . .

  • 1
    Welcome to Stack Overflow! While links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Sep 20 '18 at 06:32