Home Answers Viewqa Java-Beginners List to be visible only when entering any string into textfield

 
 


NIJIN P P
List to be visible only when entering any string into textfield
1 Answer(s)      a year and 8 months ago
Posted in : Java Beginners

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 Pages:
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
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?  
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
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
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
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
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
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
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
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
Making a Collection Read-Only
for making it read-only. By default, list is not read-only. If the list is read-only then you can not add or change any element of the list. In the given example, you can see that a list has been made read-only using the unmodifiableList() method
Circular Array List - java tutorials
java.util.ArrayList is so bad as a FIFO queue when the list contains a lot.... This means // that the list is empty when head == tail. It also means... ArrayListTest { public static String testList(List list) { StringBuffer result
iphone TextField
are not editing any field, it will be in Edit when user touches the edit button...iphone TextField In this tutorial we will create table view and in table view... this: My project name is TextField and is based on Navigation Based Application
Not able to filter rows of jtable with textfield in netbeans
Not able to filter rows of jtable with textfield in netbeans  ...]; String date = null; for (int i = 0; i &lt; alstStock.size(); i...] = objBean.getQtyInItem(); data[i][9] = objBean.getQtyInBox(); } String header
Not able to filter rows of jtable with textfield in netbeans
Not able to filter rows of jtable with textfield in netbeans  ...]; String date = null; for (int i = 0; i &lt; alstStock.size(); i...] = objBean.getQtyInItem(); data[i][9] = objBean.getQtyInBox(); } String header
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
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'>
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      
List of checkboxes - JSP-Servlet
List of checkboxes  Hi! I appreciate your effort for giving the response but now it displays only the last value of ArrayList. In other words...; When    Starts: :   [24HH:MM]    
Cookie Example to Store and Show only 10 values
Cookie Example to Store and Show only 10 values   ... cookie from the list. Cookies age can be set using the following code:  setMaxAge(365*24*60*60) User can add new cookie by entering the cookie
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
enabling and disabling a hyperlinks using javascript only
be enabled.And when the link is enabled only the page 2.jsp should be displayed..." value="List Of Owners" id="listOfOwners" name="listOfOwners" onClick... Java script only to achieve the problem described in http://www.webdeveloper.com
TextPad Editor + JDK
on the downloaded file and following the prompts. Do this only after you have installed the Java JDK. 3. Entering, compiling, and executing your Java... and you will see your text output, followed by "Press any key to continue
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
Allow to Enter only numeric data.
Allow to Enter only numeric data.  hi....... I want to ask that in my project i have a textfield where user is going to enter the data so i want that the user should allow to enter only numeric data. I mean to say that if user
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
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from the database) My question is when i select a particular value it should be pass
how to update checkbox list in database
how to update checkbox list in database  Issues: i am using different checkboxs for getting role then all role list store in one string array i want... new entry.plz kindly help about this issues( when i am inserting any record
Given a list of responsibilities, identify which belong to the deployer, bean provider, application assembler, container provider, system administrator, or any combination.
Given a list of responsibilities, identify which belong... administrator, or any combination. Prev Chapter 13. Enterprise Bean Environment Next    Given a list
Applet run with appletviewer but not in browser, any code problem.
Applet run with appletviewer but not in browser, any code problem.  Hi,My problem is when I am running my applet in appletviewer index.html, Its work... a CardDemoGUI constructor from CardDemo class constructor, because when I am comiling
Summary - String
efficient when building up a string from many strings. Character has many useful... to String, where x is any type value (primitive or object). s = ...Java: Summary - String String Concatenation Following table shows how
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+""
Tomahawk selectManyMenu tag
". This renders the menu of options where only one is visible at a time...; <center>Choose any one from the list ahead : <select id="...; is that listbox can be set to any size and menu is always set to size "1
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
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
Java List Iterator Example
data into it as // Declaring ArrayList object of type String List myList = new ArrayList(); Then add some elements of type String into it as // Adding...Java List Iterator Example In Java Collection framework every classes provides
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
Java list files
, you can display list of files from any directory. Output...Java list files In this section, you will learn how to display the list... only the files, we have created a condition that if the array of files contains
regarding connectivity program only - JDBC
end data base using sql quires ... when i click the button total i should get... class DatabaseConnect{ public static void main(String[] args) { System.out.println("MySQL Connect Example."); Connection con = null; String url
timezone_identifiers_list
]] ) array timezone_identifiers_list ([ int $what = DateTime::ALL [, string... option country is only used when what is set to DateTimeZone::PER_COUNTRY...PHP timezone_identifiers_list() Function timezone_identifiers_list alias
How to Type in japanese in java Textfield?
How to Type in japanese in java Textfield?  Hello Sir/madam; Am creating an application in java in which i want to get a japanese String input from... in textfield ..Please help me and tell me how to write in japanese
contains for list in struts2 - Struts
contains for list in struts2  Is there any way to check in the list or for existence of an enum ? Something like list.contains(string). iterating over list I want to do something like Display x display y
String and StringBuffer - Java Beginners
. Simply stated, objects of type String are read only and immutable. The StringBuffer... than String when performing simple concatenations. StringBuffer is mainly... a String, but can be modified. At any point in time it contains some particular
Use Ordered List In JSP
Use Ordered List In JSP   I created a Sting that contains ... . This string is generated dynamically from the db. When I place the string in the jsp, it does not create the ordered list. I can paste that string in the jsp
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
list - Java Interview Questions
if and only if this list contains at least one element e. import java.util.*; class ListExample { public static void main(String[] args) { List...list  Hi all Naturally in java a list will allow duplicates
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
Getting content of alert message into textfield
of alert message into textfield using javascript   Getting content... Validation{ public static void main(String[] args){ JFrame f=new JFrame...()){ String st="You have selected radiobutton instead

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.