How do you make Jtextfield behave like Combobox?

Asked

Viewed 40 times

0

I wonder if there’s a way to turn my Jtextfield into some sort of combo box.

I would like that when I type something, this data is automatically relating to some database and appear options of choice for me, as happens in the address bar of a browser for example.

I would just like an example from a simple Jtextfield of how I can make that list of options appear.

Simple code:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/* Name of the class has to be "Main" only if the class is public. */
class NewClass
{
    public static void main (String[] args) throws java.lang.Exception
    {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JTextField(), BorderLayout.NORTH);
        frame.setSize(300, 300);        
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}
  • 1

    See the answer of diegoaugusto, is exactly what you need

  • Exactly what I wanted, thank you very much! @Articuno

No answers

Browser other questions tagged

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