Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT About jcombobox
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


pranjal
About jcombobox
2 Answer(s)      5 years and 3 months ago
Posted in : Swing AWT



Advertisement
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  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
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
have jcombobox on Jframe form of IDE. In which the values are to be loaded from... change the selected item in JComboBox. Thank you.   import java.sql....); JLabel lab=new 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 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
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
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 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
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
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
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
Advertisements
 

 

 

DMCA.com