how to do auto complete with jlist or combobox...actually using in the combobox i have to get the name associated with the entered first letter only....if using list i have to get only the selected string the entire other names in the list must be hidden and the list must be visible only when i am entering anything into the textfield...please do help...
September 26, 2011 at 10:27 AM
package tests;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JPopupMenu;
public class PopUpMenu1 extends javax.swing.JFrame {
DefaultListModel lsm = new DefaultListModel();
JPopupMenu jpm = new JPopupMenu();
ArrayList<String> arls;
/** Creates new form PopUpMenu1 */
public PopUpMenu1() {
jList2 = new javax.swing.JList();
jList2.setModel(lsm);
jList2.setSelectedIndex(1);
//jScrollPane1.setViewportView(jList1);
arls = new ArrayList();
for (int i = 0; i < 10; i++) {
arls.add(i + "abcd" + i);
lsm.addElement(i + "abcd" + i);
}
jpm.add(jList2);
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
jCheckBox1 = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setComponentPopupMenu(jpm);
jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(javax.swing.event.CaretEvent evt) {
jTextField1CaretUpdate(evt);
}
});
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jTextField1FocusGained(evt);
}
});
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField1KeyReleased(evt);
}
});
jList1.setModel(lsm);
jScrollPane1.setViewportView(jList1);
jButton1.setText("jButton1");
jCheckBox1.setText("jCheckBox1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(35, 35, 35).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jScrollPane1).addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)).addGap(40, 40, 40).addComponent(jButton1))).addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(25, 25, 25).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(35, 35, 35).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addGap(52, 52, 52).addComponent(jButton1)))).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jCheckBox1))).addContainerGap(81, Short.MAX_VALUE)));
pack();
}// </editor-fold>//GEN-END:initComponents
private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusGained
// TODO add your handling code here:
jpm.show(this, jTextField1.getX() + 50, jTextField1.getY() + 50);
jTextField1.requestFocus();
//jpm.setVisible(false);
//jTextField1.setComponentPopupMenu(jpm);
}//GEN-LAST:event_jTextField1FocusGained
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
// TODO add your handling code here:
if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
// Do stuff.
if (jList2.getSelectedIndex() == lsm.getSize() - 1) {
jList2.setSelectedIndex(0);
} else {
jList2.setSelectedIndex(jList2.getSelectedIndex() + 1);
}
}
if (evt.getKeyCode() == KeyEvent.VK_UP) {
if (jList2.getSelectedIndex() == 0) {
jList2.setSelectedIndex(lsm.getSize() - 1);
} else {
jList2.setSelectedIndex(jList2.getSelectedIndex() - 1);
}
}
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
jTextField1.setText((String) jList2.getSelectedValue());
}
}//GEN-LAST:event_jTextField1KeyReleased
private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
// TODO add your handling code here:
String starts = jTextField1.getText();
lsm.removeAllElements();
for (String elem : arls) {
if (elem.startsWith(starts)) {
lsm.addElement(elem);
}
}
jTextField1.requestFocus();
}//GEN-LAST:event_jTextField1CaretUpdate
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PopUpMenu1().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
private javax.swing.JList jList2;
}
Related Tutorials/Questions & Answers:
Advertisements
list files only - Java Beginnerslist files only i have to
list only files and number of files in a directory?
i use
list() method but it returns both files and directories in parent directory.are there
any specific methods that show
only files?
are file
Declaring string array of same data type.
Suppose if we have a declare an array of type
String, then it will store
only
the
String value not
any other data type.
When we have a closely...Declaring
string array
docker list only stopped containersdocker
list only stopped containers Hi,
I am new to docker... to
list only stopped containers in Docker?
Thanks
Hi,
The docker... the
list of all the stopped containers.
After getting the
list of all the stopped
How to play only video file from any linkHow to play
only video file from
any link Hi Friends Please Help Me.
My requirement is to play
only videos from
any webpages(ITS NOT A ABSOLUTE PATH)[IF I PASTE
any VRL---,IT HAS TO BE PLAY IN
ANY CONTROL] how it possible
Array List and string operationArray
List and
string operation hi,
I want to search an arraylist element in a a given file.
example I have a name called "mac" in my arraylist and I have a txt file which contains mac, how many times "mac" appears in the txt
Validate textfield in Java Swing to validate the
textfield by allowing
only numbers to enter. For this purpose, we have... allowed the user to enter
only numeric digits from 0 to 9.If the
user enters... keyPressed(KeyEvent EVT) {
String value = text.getText();
int l
TextField validationsTextField validations I want to know How to check first letter...
textfield?
and also want to know how to check all digits or not in a
textfield?
ex:
when we enter data in age field we want check they are digits or not?
TextField validationsTextField validations I want to know How to check first letter...
textfield?
and also want to know how to check all digits or not in a
textfield?
ex:
when we enter data in age field we want check they are digits
ModuleNotFoundError: No module named 'VISIBLE'ModuleNotFoundError: No module named '
VISIBLE' Hi,
My Python... '
VISIBLE'
How to remove the ModuleNotFoundError: No module named '
VISIBLE... to install padas library.
You can install
VISIBLE python with following command
using getText(String aTextName, List args) - Strutsusing getText(
String aTextName,
List args) What should my pacakge.properties (Message.properties) should have ?
basically i am trying to use ActionSupport.getText(
String aTextName,
List args)
I want something like "you
Java reverse words in a string using only loopsJava reverse words in a
string using
only loops
In this tutorial, you will learn how to reverse words in a
string without
using
any inbuilt methods like split() etc, StringTokenizer functiom or
any
extra ordinary function
Only loops
TextField
TextField
... as the
TextField.
In the example given below, shown a
textfield in the applet by creating its...;new
TextField("Type in the box");
Java textfield validatonJava
textfield validaton Sir... i'm new to java. In java JTextfield
when we entered a negative value after pressing one button it have to display an error message dialog of
textfield and also the
textfield should accept
Java textfield validatonJava
textfield validaton Sir... i'm new to java. In java JTextfield
when we entered a negative value after pressing one button it have to display an error message dialog of
textfield and also the
textfield should accept
string string difference detween "public static void main (
String[] args) " and
"public static void main (
String args[])" in java but it executes both ,in java which one is legal
The
only difference is style, where
IPhone-UIPicker Data to TextFieldIPhone-UIPicker Data to TextField
When I select the
textfield, a picker view must come in form of action sheet and the data selected from that picker view must be populated in the text field. can anyone help me on this please
java textfield problemjava
textfield problem I want to navigate the content of text field from one class to another and viceversa through button but
when I am getting...(ActionEvent e)
{
String text=tf1.getText();
new
Converting object to String because you need to pass it to
the method that accept
String only. Object is a class....
The valueOf() - If u want to convert
any variable to
string
then this method...Converting object to
String
In this section you will learn to convert Object
how to set size of button,textfield.how to set size of button,
textfield. how we set size of button
textfield etc and reduce problem
when click maximize button the text or
textfield get large how to set as a preferd size in the panel or frame .
when we click
Integer value to string - Swing AWT into
String ,so that we can select some integers from
list and sum them and add to a
textfield. Here the problem is
when we are going to add Integer to
TextField. int i=10;
String str =String.valueOf(i);orString str =i+""
stringstring a java program using
string function to input
any string... ArrangeStringAlphabetically
{
public static void main(
String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter
string how to move pointer in Textfield to newlinehow to move pointer in
Textfield to newline consider my case...;
class Table1 extends Frame implements ActionListener{
TextField tf1,tf2,tf3...");
l2.setBounds(100,50,200,20);
tf1=new
TextField();
tf1.setBounds(100,80,200,20
StringString write down the code of remove
any character from a given
string without using
any string function
please give me the code of remove
any given character from a given
string without using function
validation before entering values into database in jspvalidation before
entering values into database in jsp Hi,
my project involves
entering data into database from jsp form.but before
entering into database it should pop up a alert message that "you have selected product:name
string and also displaying that contain word?
Like I want to find "a" in the
string... and a character. It then count the occurrence of that character in the
string and display...
String_Example {
public static void main(
String[] args
stringstring program for
String reverse and replace in c,c++,java...(
String[] args)
{
String array[]=new
String[5];
Scanner input=new Scanner(System.in);
System.out.println("Enter
List Elements