How could I implement autocompletion using Swing

0 votes
I'm interested in providing an autocompletion box in a JFrame. The triggering mechanism will be based on mnemonics (I think), but I'm not really sure what to use for the "autocompletion box" (I would like results to be filtered as the user presses keys).

How would you implement this? Some sort of JFrame, or a JPopupMenu?
Apr 21, 2020 in Java by kartik
• 37,510 points
430 views

1 answer to this question.

0 votes

Hello,

You can try out this:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Autocompleter2
{
    //~ Methods ------------------------------------------------------------------------------------

    public static void main(String[] args)
      throws Exception
    {
        // YES, IT'S EMPTY !!!
        // It'll start anyway because of static initializers
        SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    final JPopupMenu textPopupMenu = new JPopupMenu("MENU")
                    {

                        {
                            add(new JMenuItem("item 1"));
                            add(new JMenuItem("item 2"));
                            setFocusable(false);
                        }
                    };

                    final JTextArea textInput = new JTextArea("type something la")
                    {

                        {
                            setCaretPosition(getText().length());
                        }
                    };
answered Apr 21, 2020 by Niroj
• 82,880 points

Related Questions In Java

0 votes
1 answer

How do I implement a HttpURLConnection using a Proxy ?

Since java 1.5 you can also pass ...READ MORE

answered Dec 10, 2018 in Java by Sushmita
• 6,910 points
10,181 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,580 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,708 views
0 votes
1 answer

How can I create an executable JAR with dependencies using Maven?

<build> <plugins> <plugin> ...READ MORE

answered Apr 26, 2018 in Java by sophia
• 1,400 points
1,072 views
0 votes
1 answer

What are the prerequisites to learn Hadoop in java perspective?

In my day job, I've just spent ...READ MORE

answered Oct 11, 2018 in Big Data Hadoop by Frankie
• 9,830 points
688 views