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
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


NIJIN P P
List to be visible only when entering any string into textfield
1 Answer(s)      3 years 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 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
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
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
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
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
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
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
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
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...;% 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
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... 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
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'>
Advertisements
 

 

 

DMCA.com