I am trying to build a suggestion JList
which is visible once the user types in certain keys like e
or a
or p
etc in the JTextField
box present in JPanel
.
I tried to use Keystroke to implement this and in actionPerformed
placed the JList
. Before the Jlist
I had a System.out statement "E is pressed."
My problem is on pressing E, the console prints "E is pressed". however the Jlist
is not appearing.Part of my code in the constructor is as follows
public snippet() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setFocusable(true);
contentPane.requestFocusInWindow();
KeyStroke stroke = KeyStroke.getKeyStroke("E");
JTextField textfield = new JTextField();
textfield.setFocusable(true);
textfield.requestFocusInWindow();
textfield.setBounds(86, 94, 1003, 38);
textfield.setToolTipText("Please enter the query.");
Action a = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Got an E");
JList list = new JList(query);
list.setFont(new Font("Times New Roman", Font.BOLD, 25));
list.getBorder();
list.setBounds(84, 212, 366, 155);
list.setVisible(true);
list.setFocusable(true);
contentPane.add(list);
}
};
InputMap inputMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, "OPEN");
contentPane.getActionMap().put("OPEN", a);
}
Any form of help is very much appreciated.I understand that KeyBinding is working as console prints the SYS out statement but I cannot figure out why Jlist is not appearing.
Thank you.