JTextField with drop down list box
hello all, may i know how can i have a JTextField component, where it
is able to display the JList drop down from time to time just like the
one in
http://www.google.com/webhp?complete=1&hl=en
Currently, the engine which is used to performed word completion is
completed. I just lacked of a gui component.
I do not want to use JComboBox. I do not want to have the arrow at the right.
Thank you.
cheok
[459 byte] By [
KwangHooia] at [2007-11-27 2:41:13]

# 2
have a look at this code and adapt it to your needs:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
public class SuggestListProject extends JPanel{
private JTextField text;
final JPopupMenu menu = new JPopupMenu();
public SuggestListProject(){
setLayout(new GridLayout(2, 1));
text = new JTextField(50);
text.addKeyListener(new KeyAdapter(){
@Override
public void keyReleased(KeyEvent e) {
redrawSuggestionList();
}
});
add(text);
add(menu);
}
private void redrawSuggestionList(){
text.requestFocus();
menu.setVisible(false);
menu.removeAll();
String query = text.getText();
if(query!=null&&query.trim().length()>0){
for (int i = 0; i < 3; i++) {
JMenuItem item = new JMenuItem(query+" : "+i);
item.addActionListener(new CustomActionListener(i));
menu.add(item);
}
}
menu.show(text, text.getLocation().x-text.getWidth(), text.getLocation().y+text.getHeight());
text.requestFocus();
}
class CustomActionListener implements ActionListener{
private int index;
public CustomActionListener(int index){
this.index=index;
}
public void actionPerformed(ActionEvent e) {
System.out.println(" Index : "+index);
}
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SuggestList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new SuggestListProject();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}