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



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

Have Programming Question? Ask it here!
 
 
 


NIJIN P P
List to be visible only when entering any string into textfield
1 Answer(s)      2 years and 10 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...


Advertisement
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
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
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
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
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 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
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      
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?  
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
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
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
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
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
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 on the Image
TextField on the Image  how we set label or textfield etc. on the Image
diff betn show n visible
diff betn show n visible  what is difference between show() & visible method in java
TextField
TextField      ... as the TextField. In the example given below, shown a textfield in the applet by creating its...;TextField("Type in the box");    add(tf); 
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
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 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
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
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
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
-[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
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
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 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
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.... Example of remove any given character from a string
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
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
textfield selected text
textfield selected text  How to select text in text field
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
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
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
List
List  i do have one list object, i want to compare this list from database table and if match found i want to update the list
s:textfield - Struts
s:textfield  I am using the s:textfield tag in an appication and need... this?  Hi friend, Textfield Tag Example Textfield Tag Example
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
List
List  getting values from a form and make a list of them and store them into database
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
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
JDialog to create a customized dialog box for entering text data
JDialog to create a customized dialog box for entering text data   Hello sir I wanted to know the code to generate a JDialog to create a customized dialog box for entering text data. The dialog can be split into panes, left pane
get textfield value in javascript
get textfield value in javascript  How to get and the set value of text field in JavaScript?   Get the text field value in JavaScript <html> <head> <script type="text/javascript"> <
how to set the value of textfield constant
how to set the value of textfield constant  how to set the value of textfield constant
autocompletion in textfield fetching from database in jsp
autocompletion in textfield fetching from database in jsp  hi i want to retrieve country name from mysql db to textfield.. maeans if anyone type first letter of the name it should show the corresponding names in the list
List in J2ME
List in J2ME       Exclusive List MIDlet Example This example illustrates how to create a Exclusive List. The Exclusive List is used to select only one list element at a time
Advertisements
 

 

 

DMCA.com