0

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. – Andrew Thompson Apr 19 '14 at 02:03
  • .. 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 4) Probably best to add the list at start-up, and adjust the model (e.g populate it) on action performed. – Andrew Thompson Apr 19 '14 at 02:04
  • Why do you have 2 `requestFocusInWindow()` statements. First of all that method only works on components displayed on a visible GUI. It does not work when you just create the component. Secondly, you can't have focus on 2 components at once, so why even try? Most components are focusable by default, so you don't need use setFocusable(true). A panel is not focusable, but that doesn't matter because you are using Key Bindings. Also, Swing components (except top level containers) are visible by default so you don't need setVisible(true). What is the point of the getBorder() statement? – camickr Apr 19 '14 at 02:28
  • Thank for the replies. I will try the cardLayout Example.I understand that there are few unnecessary focus statement.I will remove those.getBorder i was trying to see if there was any problem with data population or atleast an empty list is being displayed with border. – user3550485 Apr 19 '14 at 02:59
  • *"Thank for the replies."* Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 19 '14 at 03:51

0 Answers0