List to be visible only when entering any string into textfield

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...

View Answers

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:
List to be visible only when entering any string into textfield
List to be visible only when entering any string into textfield  how... 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
how to enable textfield in j2me netbeans when a choice element is selected?
how to enable textfield in j2me netbeans when a choice element is selected?  How to enable a textfield in j2me using netbeans when a choice element is selected and how to keep it disabled when any choice element corresponding
Advertisements
how to enable textfield in j2me netbeans when a choice element is selected?
how to enable textfield in j2me netbeans when a choice element is selected?  How to enable a textfield in j2me using netbeans when a choice element is selected and how to keep it disabled when any choice element corresponding
list files only - Java Beginners
list 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 containers
docker 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 move curosr from one text field to another automatically when the first textfield reaches its maximum length
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length  how to move curosr from one text field to another automatically when the first textfield reaches its maximum
How to play only video file from any link
How 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 operation
Array 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 validations
TextField 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?  
i have one txt field and one button.when i entere any test in testfield then only button should be enabled.
i have one txt field and one button.when i entere any test in testfield then only button should be enabled.  i have one txt field and one button.when i entere any test in testfield then only button should be enabled. i need
TextField validations
TextField 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
display a list of names(when we press first letter)
display a list of names(when we press first letter)  If i gave...; import javax.swing.*; class Name { public static void main (String arg[]) { String st=JOptionPane.showInputDialog(null,"Enter any
TextField on the Image
TextField on the Image  how we set label or textfield etc. on the Image
How to Manipulate List to String and print in seperate row
How to Manipulate List to String and print in seperate row  I am... is a List. String appname = "abc"; String path = "//home/exportfile... String userid=""; List links = user.getLinks
Write a method which will remove any given character from a string?
Write a method which will remove any given character from a string? In this example we will describe remove any given character from a string... sequence.ADS_TO_REPLACE_1 Example of remove any given character from a string
how to highlight the field in image,when i have entered into that corresponding field's textbox-any one help out
how to highlight the field in image,when i have entered into that corresponding field's textbox-any one help out  how to highlight the field in image,when i have entered into that corresponding field's textbox. Here webpage
using getText(String aTextName, List args) - Struts
using 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
i have one txt field and one button.when i entere any test in testfield then only button should be enabled. i need for this
i have one txt field and one button.when i entere any test in testfield then only button should be enabled. i need for this   i have one txt field and one button.when i entere any test in testfield then only button should
Java reverse words in a string using only loops
Java 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 validaton
Java 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 validaton
Java 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
what is the differnce between the number format and string format when creatign the table in oracle - JDBC
what is the differnce between the number format and string format when... the number format and string format when creatign the table in oracle. if i enter the number in string format,can i know the advantage if i enter the number
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list...)&& ($select!="")){ $select=$_POST['NEW']; } ?> <?php $list
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list...)&& ($select!="")){ $select=$_POST['NEW']; } ?> <?php $list
to update drop down list value when selected from website
to update drop down list value when selected from website  hi help me, i want to insert value into db when i select from a drop down list...)&& ($select!="")){ $select=$_POST['NEW']; } ?> <?php $list
diff betn show n visible
diff betn show n visible  what is difference between show() & visible method in java
data should not repeat in the drop down list when it is loading dynamically from database
data should not repeat in the drop down list when it is loading dynamically...;% String no=request.getParameter("count"); String buffer="<select name... the database but when we give same department name it is repeting i.e if we enter
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
data should not repeat in the drop down list when it is loading dynamically from database
data should not repeat in the drop down list when it is loading dynamically... name is given it has to come only once and when new department is entered it should... the whole department field from the data base (i.e in the drop down list
IPhone-UIPicker Data to TextField
IPhone-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 problem
java 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
data should not repeat in the drop down list when it is loading dynamically from database
data should not repeat in the drop down list when it is loading dynamically... import="java.sql.*"%> <% String no=request.getParameter("count"); String buffer="<select name='emp' ><option value='-1'>
how to call list objects as string into my sql query?
how to call list objects as string into my sql query?  how to call list data as string into my sql query so that i can retrieve all values from database? List has data retrieved from xml(using xmlSAXparser
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box and a text box. I am sending an Array List object from a servlet to this jsp
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+""
string
string  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 newline
how 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
String
String  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
ModuleNotFoundError: No module named 'openerp-product-visible-discount'
ModuleNotFoundError: No module named 'openerp-product-visible-discount' ...: ModuleNotFoundError: No module named 'openerp-product-visible-discount' How to remove the ModuleNotFoundError: No module named 'openerp-product-visible-discount'
textfield selected text
textfield selected text  How to select text in text field
validation before entering values into database in jsp
validation 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
-[UIPopoverController dealloc] reached while popover is still visible.
-[UIPopoverController dealloc] reached while popover is still visible.  Hi, I am getting following error in my application: -[UIPopoverController dealloc] reached while popover is still visible. How to solver this? Thanks
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
string
string  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