简体   繁体   中英

How to make a dropdown in Swing?

I need to make a text box which autocompletes what the user is typing. I want something like a JComboBox that can update whenever the user types a new character based off of a set of possibilities. I want to do this by creating my own custom component by either extending JPanel or JComponent. What I am unsure about is how do I make a frame which can float over all other content. Like when you hit the drop down for a JComboBox how can I put a list of possibilities which floats above the background and underneath the text box?

You need import SwingX (last version) on your project:

import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

AutoCompleteDecorator this class contains only static utility methods that can be used to set up automatic completion for some Swing components.

Pass your JComboBox to the static method:

jComboBox1 = new javax.swing.JComboBox();
jComboBox1.setEditable(true);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "alejandro", "berenice", "juan", "ana", "bartolo", "diana", "cesar" }));
jComboBox1.setName("jComboBox1");
AutoCompleteDecorator.decorate(this.jComboBox1);

尝试使用jide组件,它们是基于swing的,并且提供比swing更高级的gui功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM