About jcombobox

About jcombobox

View Answers

October 13, 2009 at 11:38 AM

Hi Friend,

Try the following code:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class AutoSuggest extends JPanel{
private final JTextField tf;
private final JComboBox combo = new JComboBox();
private final Vector<String> v = new Vector<String>();
public AutoSuggest() {
super(new BorderLayout());
combo.setEditable(true);
tf = (JTextField) combo.getEditor().getEditorComponent();
tf.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
String text = tf.getText();
if(text.length()==0) {
combo.hidePopup();
setModel(new DefaultComboBoxModel(v), "");
}else{
DefaultComboBoxModel m = getSuggestedModel(v, text);
if(m.getSize()==0 || hide_flag) {
combo.hidePopup();
hide_flag = false;
}else{
setModel(m, text);
combo.showPopup();
}
}
}
});
}
public void keyPressed(KeyEvent e) {
String text = tf.getText();
int code = e.getKeyCode();
if(code==KeyEvent.VK_ENTER) {
if(!v.contains(text)) {
v.addElement(text);
Collections.sort(v);
setModel(getSuggestedModel(v, text), text);
}
hide_flag = true;
}else if(code==KeyEvent.VK_ESCAPE) {
hide_flag = true;
}else if(code==KeyEvent.VK_RIGHT) {
for(int i=0;i<v.size();i++) {
String str = v.elementAt(i);
if(str.startsWith(text)) {
combo.setSelectedIndex(-1);
tf.setText(str);
return;
}
}
}
}
});

October 13, 2009 at 11:39 AM

continue....

String[] countries = {"Afghanistan", "Albania", "Algeria", "Andorra", "Angola","Argentina","Armenia","Austria",
"Bahamas","Bahrain", "Bangladesh","Barbados", "Belarus","Belgium","Benin","Bhutan","Bolivia",
"Bosnia & Herzegovina","Botswana","Brazil","Bulgaria","Burkina Faso","Burma","Burundi",
"Cambodia","Cameroon","Canada", "China","Colombia","Comoros","Congo","Croatia","Cuba",
"Cyprus","Czech Republic","Denmark", "Georgia","Germany","Ghana","Great Britain","Greece","Hungary",
"Holland","India","Iran","Iraq","Italy","Somalia", "Spain", "Sri Lanka", "Sudan","Suriname", "Swaziland",
"Sweden","Switzerland", "Syria","Uganda","Ukraine","United Arab Emirates","United Kingdom",
"United States","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","Yemen","Zaire","Zambia",
"Zimbabwe"};
for(int i=0;i<countries.length;i++){
v.addElement(countries[i]);
}
setModel(new DefaultComboBoxModel(v), "");
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder("AutoSuggestion Box"));
p.add(combo, BorderLayout.NORTH);
add(p);
setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
setPreferredSize(new Dimension(300, 150));
}
private boolean hide_flag = false;
private void setModel(DefaultComboBoxModel mdl, String str) {
combo.setModel(mdl);
combo.setSelectedIndex(-1);
tf.setText(str);
}
private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) {
DefaultComboBoxModel m = new DefaultComboBoxModel();
for(String s: list) {
if(s.startsWith(text)) m.addElement(s);
}
return m;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new AutoSuggest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Thanks









Related Tutorials/Questions & Answers:
About jcombobox - Swing AWT
About jcombobox  Hi all, I am new to this forum. Can we do auto suggest jcombobox in swing? If yes is there any jar file for that or any code... JTextField tf; private final JComboBox combo = new JComboBox(); private
JComboBox
JComboBox  I want to change the index of one jComboBox according to the selected index of another jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
Advertisements
jComboBox
jComboBox  I want to change the index of one jComboBox according to the selected index of another jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
JComboBox
JComboBox  I have jcombobox. In which tha values are loaded from MySql Database. My problem is that i want to load content of the jtable whenever i change the selected item. Please some one help me to do this. Thank you
jcombobox
jcombobox  hi i have developed a form and i have jcombobox in which data is retrieved from ms access but the problem is that if we pressed the down key the last 5 data are not showed only the other data can be pressed
JComboBox
have jcombobox on Jframe form of IDE. In which the values are to be loaded from... change the selected item in JComboBox. Thank you. (adsbygoogle... JLabel("Select"); final JComboBox combo=new JComboBox
Jcombobox
Jcombobox  Hii, I am doing my final year project and i am using java swing as a front end.I have used Jcombobox for displaying my on bluetooth devices and i also made a refresh button.when i click on the refresh button i
Create a JComboBox Component in Java
Create a JComboBox Component in Java       In this section, you will learn about the JComboBox Component of swing in java. The JComboBox is used to display drop-down list
issue on jcombobox
issue on jcombobox  i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox.only few values in the combobox need this functionality. need
JComboBox Insert Edited Value Into Table
JComboBox Insert Edited Value Into Table In this section we will read about how to make JComboBox an editable and then how to insert the new edited value... in to insert an editable value of JComboBox into a table. We will use an Java editor
jComboBox with database
jComboBox with database  Hello friends, I have created three JComboBoxes. first two of them get list from string. However I select these two comboboxes, the third combobox automatically retrieve data of a particular column from
JComboBox on JRadioButton - Java Beginners
JComboBox on JRadioButton  How to add JComboBox on Jpanel ,Give Me Sample Code
Java JComboBox Get Selected Item Value
Java JComboBox Get Selected Item Value In this section we will discuss about how to get the selected item value form JComboBox. javax.swing.JComboBox.... In this tutorial we are discussing about how to store the selected item value
Help on database and JComboBox
Help on database and JComboBox  I want to select from the JComboBox and when click onto the "search" button, the selected category (example "new york") all the each new york picture and detail will be seen. But how to grab
JComboBox with AccessDatabase - Java Beginners
JComboBox with AccessDatabase  Hello Sir I have Created Course... on Student Admission Form when I select Course Name from JComboBox on Student..."); JComboBox combo1=new JComboBox(); JComboBox combo2=new JComboBox
JComboBox Event Handling - Java Beginners
JComboBox Event Handling  Hi, I have problem with event handling here.There are Two JComboBox and a JTextField.When I select an item in first... Compare extends JFrame implements ItemListener { JComboBox combo,lcombo
problem with addactionlistener on JComboBox - Java Beginners
[] = { "A", "B", "C"}; JFrame frame = new JFrame(); JComboBox combo = new JComboBox(st); frame.add(combo); ActionListener actionListener = new
Changing JLabel with a jcombobox - Java Beginners
listener event of JComboBox put JLabel.setText("Put yourtext here
Link To Database with JComboBox - Java Beginners
course names from Access Database in to JComboBox but now I want to do ,when i select Course name from Jcombobox i want to display appropriate records relate..."); final JComboBox jc=new JComboBox(); JLabel lbl2 = new JLabel("Duration"); final
JComboBox Display Problem - Java Beginners
JComboBox Display Problem  I am create one program that contain two...,ItemListener { Container c; JLabel lblstart,lblend; JComboBox cmbstart..."); lblend=new JLabel("End Date"); cmbstart=new JComboBox(); cmbend=new
How to store JComboBox item into database
How to store JComboBox item into database   import... DefaultComboBoxModel(labels); final JPanel TypeTF = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setEditable(false
Database values in JComboBox
Database values in JComboBox In this section, you will learn how to display values in JComboBox from database. For this, we have allowed the user to enter... JFrame(); f.getContentPane().setLayout(null); final JComboBox combo = new
easy way to make a JCombobox look like a JTextField?
easy way to make a JCombobox look like a JTextField?  Is there a good(and easy) way to make a JCombobox look like a JTextField? By this I mean there should not be a dropdown button, but when the user enters something it should
binding jComboBox to mysql database - Swing AWT
binding jComboBox to mysql database  I am using netbeans 6.5 How to populate jComboBox with data of specific column of mysql database table? I...){ e.printStackTrace(); } JComboBox comboTypesList = new
How to Add JComboBox on JPanel - Java Beginners
How to Add JComboBox on JPanel  How to Add JComboBox on JPanel D:\java>javac ViewElements.java ViewElements.java:181: expected...); JLabel lbl4=new JLabel("Branch"); final JComboBox jc=new JComboBox(); jc.addItem
Add Items in JComboBox from Access Database - Java Beginners
Add Items in JComboBox from Access Database  Heelo Sir I want To add ittem in JComboBox which is stored in Access Database. plz Help Me Sir... records in to my JComboBox.   Hi Friend, Try the following code
How to store JComboBox selected Item into Ms Access Database - Java Beginners
How to store JComboBox selected Item into Ms Access Database  How to store JComboBox selected Item into Ms Access Database.  Hi Friend..."); f.getContentPane().setLayout(null); JLabel lbl1=new JLabel("Branch"); final JComboBox jc
How to save JCombobox Selected Item in to Access Database - Java Beginners
How to save JCombobox Selected Item in to Access Database  How to save JCombobox Selected Item in to Access Database  Hi Friend, Try...().setLayout(null); JLabel lbl1=new JLabel("Branch"); final JComboBox jc=new
About struts
About struts  How will we configure the struts
About Java
About Java  Hi, Can anyone tell me the About Java programming language? How a c programmer can learn Java development techniques? Thanks   Hi, Read about java at http://www.roseindia.net/java. Thanks
Load Coursenames from MS Acess Database to JComboBox - Java Beginners
"); final JComboBox jc=new JComboBox(); try{ Class.forName
navigation between panels when item is selected from jcombobox - Swing AWT
on JComboBox in Java visit to : http://www.roseindia.net/java/example/java/swing
about java swing - Java Beginners
about java swing   How to send date in data base if i use... JFrame(); f.getContentPane().setLayout(null); final JComboBox day=new JComboBox(); final JComboBox month=new JComboBox(); final JComboBox year=new JComboBox
About jsp
About jsp  Read Excel data using JSP and update MySQL databse
about java
about java  how to get the value from the user like c, c++ program pls explain with example for me
about jquery
about jquery  i want to put already made dropdown menu code which is in internet into my website.but how to put
About Java2
About Java2  sir i want to a develop one text editor but the problem is that i m not able to save our file as text formet or any formet Sir plz guide me
about java1
about java1  Sir, i want to know how we develop 3d button ,lable,textfield etc. in java . sir plz give one program as well Thank you
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
About Main
About Main  can u create the object of one interface ? But i can able to create the abstract class through anonimous inner class.similarly can i
About Main
About Main  can u create the object of one interface ? But i can able to create the abstract class through anonimous inner class.similarly can i
about connectivity
about connectivity  hello i am basavaraj,will any one tell me how to use hibernate in struts.   Please visit the following link: Struts Hibernate Integration
About Database
About Database  in my database i enter the date of birth as Birth_Date date but i dont know in which format we can mention the date of birth   In MySQL database, date should be entered in the following format: yyyy-mm
About Jsp
About Jsp  Hello sir, I am developing online Quiz project in jsp using MySql Database. I want to know that How I will show Questions from database One by One on Same page and also want to calculate Result for the User
About Constructor
About Constructor  How many objects are create when this code will execute... String string = new String("Java is best Lang."); tell me the number of object of string which will create . All are those are eligible for garbage
about session
about session  hey i want to insert userid and username of the user who have currently loggedin in servlet using prepared statement   Please visit the following link: http://www.devmanuals.com/tutorials/java/servlet
About Project
About Project  Hello friends i want to make a project on face reconization this is my first projct so please help me that how i start my projct please tell me some working with image codeing. thanks
Combo box In Java
a value in the editable field. Combo box can be created using JCombobox in java.... Constructor of JCombobox are as follows: JComboBox() : Create a combo box with a default. JCombobox(ComboBoxModel a) : Create a combo box that accept
about a program
about a program  hi can anyone suggest program for this question.. it wil really be helpful.its based on *servlet programming* 1. First page should display a dropdown of mathematical operation (Add, Subtract, Multiply, Divide
About java
About java   how we insert our database data into the jTable in java or how we show database content into the JTable in java   Hi Friend,ADS_TO_REPLACE_1 Try the following code: import

Ads