Search code examples
javaawtjappletsearch-box

How can I make a search box in Java


I'm making a program in Java which users can login and upload data to certain people in the program. But, I want to make a search bar so someone can type 'Joe' and it would show anyone with the name 'Joe' in a file. Each name that is registered will be made a file so 'Joe Rico' would have a folder named 'Joe Rico'. I plan to use AWT for the GUI system if that matters and this will be a Java applet if that matters also. (The file system will be stored on the web-server and certain users will have certain permissions). Thanks for the help!


Solution

  • There's a lot of issues here, and your question is very vague - you could be asking any of the following questions:

    1. How do I make a search box?

      You're asking how to properly implement a search box in AWT. First off, use Swing, it's a lot nicer. From there, resources like How to use Text Fields will be very helpful.

    2. How do I make a search application?

      You're asking how to implement searching behavior in a GUI. In this case, the key concept you need to understand is Model View Controller, which will let you compartmentalize your business logic from your graphical interface in a clean and efficient way.

    3. How do I efficiently search data?

      You're asking for data structures that are good for searching. The classic answer is a Binary Search Tree or a binary search algorithm, like Collections.binarySearch(), which allows you to search in O(log n) time. This may or may not be enough for your use case, but another possible option is a Trie, which provides fast prefix-searching. If these basic data structures aren't good enough for your use case, read on.

    4. How do I store data for efficient searches?

      Very much dependent on exactly what you're trying to do, but the concept is called Serialization. Java provides serialization behavior natively, however I personally vastly prefer using JSON, and Google's awesome Gson library makes this quite painless.

    5. What tools exist to provide search behavior for me?

      If answer #3 wasn't good enough, you're looking for something more powerful. You've got two basic choices, depending on your needs. Either a Relational Database, or a Search/Index library. There are countless database options out there, but I like H2 for quick Java work. A proper search tool like Lucene is almost certainly overkill for your needs, but the option exists.

    I hope some of this answers your question. In the future, try to create questions that can have a concrete answer. Questions that are open-ended and leave the notion of "answered" undefined are very difficult to properly address.