esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I have my JList go to a specific item when a user presses a key?


JListKeyPressed.java:

 
import javax.swing.*;
import java.awt.event.*;

public class JListKeyPressed extends JFrame {
private JList list;
private JPanel p;

public JListKeyPressed() {
super("JListKeyPressed");
p = new JPanel();
final String data[] = {"ah","bh","ch","dh","eh","fh","gh","hh","ih"};
list = new JList(data);

list.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
for(int i = 0; i < data.length; i++) {
if(data[i].charAt(0) == Character.toLowerCase(e.getKeyChar())) {
list.setSelectedIndex(i);
}
}
}
});

setContentPane(p);
p.add(list);
setSize(300,300);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
});

setVisible(true);
}

public static void main(String args[]) {
JListKeyPressed j = new JListKeyPressed();
}
}




Further Information
Author of answer: Uwe Billen

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.