Tuesday, July 8, 2014
Auto Suggesions
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class ShowSuggesions {
private ArrayList<String> suggeshions;
private JTextField field;
private JTextField nextToFocus;
private int width;
private int height = 100;
private Point point;
private JFrame jWindow = new JFrame();
private JScrollPane jScrollPane = new JScrollPane();
private JList<String> jList = new JList<String>();
private int size;
public ShowSuggesions() {
}
private void addKeyEvents() {
jList.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyChar() ==10){
setSelectedValue();
}
}
});
jList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
setSelectedValue();
}
});
}
private void setSelectedValue() {
field.setText(jList.getSelectedValue());
jWindow.dispose();
if(nextToFocus!=null){
nextToFocus.requestFocus();
}
}
private void init() {
height = field.getWidth();
width = (int) field.getPreferredSize().getHeight();
Point locationOnScreen = field.getLocationOnScreen();
point = new Point((int) locationOnScreen.getX(), (int) locationOnScreen.getY() + field.getHeight());
jWindow.setAlwaysOnTop(true);
jWindow.setUndecorated(true);
jScrollPane.setVisible(true);
jScrollPane.setAutoscrolls(true);
jList.setVisible(true);
jWindow.setVisible(true);
}
public void hideFrame(){
jWindow.dispose();
}
public void show(Vector<String> suggeshions, JTextField field, KeyEvent event,JTextField nexToFocues) {
if (suggeshions != null && suggeshions.size() > 0) {
if (!jWindow.isVisible()) {
this.field = field;
init();
}
size = suggeshions.size();
jList.setListData(suggeshions);
jScrollPane.setViewportView(jList);
jWindow.getContentPane().add(jScrollPane);
this.nextToFocus = nexToFocues;
jWindow.setSize(field.getWidth(), 100);
jWindow.setLocation(point);
addKeyEvents();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment