You may want to make your JComboBox behave like a address bar of browser that displays a drop down list of matching URL’s dynamically when you enter a character or a word in the address-bar . To accomplish this .. There are plenty of libraries available on the internet .. Some are open source and some you’ll need to buy.. Let us look at one such implementation using an Open Source library called as swingX Library which is developed and maintained by a community called as SwingLabs..So Download the latest swingx library (Jar file) from the SwingLabs community website and include it in the classpath of your project .
Autocomplete JComboBox : Java Swingx Tutorial:-
Below is the sample code :-package AutoComplete ;
/**
* Demo program to Autocomplete JComboBox
* @author Pramod Choudhari
*/
public class AutoComplete extends javax.swing.JFrame {
public AutoComplete() {
initComponents();
AutoCompleteDecorator.decorate(jComboBox1); // Autocomplete jCombobox1
}
@SuppressWarnings(“unchecked”)
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText(“Elements”);
jComboBox1.setEditable(true); //must make the JcomboBox editable to enable user typing
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “Apple”, “Mango”, “Custard Apple”, “Java”, “Javac”, “Pramod”, “Pravin”, “Pragma” }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(62, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(83, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AutoComplete().setVisible(true);
}
});
}
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
}
Description :-
The above code consists of an editable JComboBox containng these elements -> “Apple”, “Mango”, “Custard Apple”, “Java”, “Javac”, “Pramod”, “Pravin”, “Pragma” that the user will see i the JComboBox of of AutoComplete JComboBox program .
AutoCompleteDecorator.decorate(jComboBox1); -> This lines depicts that AutoCompleteDecorator is a Class located in the package AutoComplete . AutoCompleteDecorator is having many static methods sand one such method is decorator( ) which Takes a JComboBox and many other text components as parameters (Text component on which autcomplete feature is to be applied as a parameter).
Screenshot (After running the above code):-
Run this code ! Done
-
https://www.facebook.com/Arun.Bisht.25 Arun