Search box in Java

Asked

Viewed 1,159 times

0

I am creating a Java system using Netbeans. I would like to create a search box that shows the results in a list (of variable size) below the box as the user enters the text to be searched. It would be something similar to the Google search box, which shows suggestions while the user is typing.

The system should also allow the user to select search results, without the other results being hidden. I would like the list of results to be on other components of the interface.

I would also like it to be possible to search from certain cells in a table and the results to be displayed below the cell used for searching.

I don’t know any class components Swing or Netbeans functionality that can do this. Someone has some idea?

  • 2

    Try to use the JComboBox.

  • I think it’s not quite what I wanted. I would like to see only the results and not all the options, as in this component. In addition, it seems that this component performs the search only at the beginning of the sentence and I would like to see results that also have the expression searched in the middle of the sentence. My idea is that the search was done in the Mysql database and that the results were returned as the user goes typing.

  • 1

    To perform a search as the user type, you can use Jcombobox as the friend @Victor Stafusa said, add a change event in the component and perform a search in the database with those that have already updated their grid for each change event that was held.

1 answer

2


What I found closest to this was the use of lib Swingx to decorate a JComboBox.

AutoCompleteDecorator.decorate(combobox);

Example:

 public class ExemploCombo {

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

    public ExemploCombo() {
        combobox = new JComboBox(new Object[]{"","Diego", "Bruno",
        "Bianca", "Fernando", "Davi"});
        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) {
        ExemploCombo d = new ExemploCombo();
    }
}

inserir a descrição da imagem aqui

Source: https://stackoverflow.com/questions/26550559/how-to-create-a-search-bar-similar-to-google-search-style-in-java-gui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.