How to create a search bar similar to google search style in JAVA GUI

I am trying to create a search function in my program, similar to the google search bar, where when the user enters it, it actually searches the database and displays the current result in the popup below JTextField

. I'm new to Java GUI programming, so I don't quite understand all Java components, so it's hard to find suitable components that suit my needs, especially the component I need to use for the dropdown dropdown below the textbox. I hope some experts can show me some insight.

+3


source to share


1 answer


The SwingX API can help solve this problem. You can use the following code to implement an auto-complete feature for an editable ComboBox.



import javax.swing.*;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import java.awt.*;
public class Demo {

    JFrame frame = new JFrame("");
    AutoCompleteDecorator decorator;
    JComboBox combobox;

    public Demo() {
        combobox = new JComboBox(new Object[]{"","Ester", "Jordi",
            "Jordina", "Jorge", "Sergi"});
        AutoCompleteDecorator.decorate(combobox);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        frame.add(combobox);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Demo d = new Demo();
    }
}

      

+5


source







All Articles