Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Give Focus to JTextField within JList

  1. #1
    sycosquirl18 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Give Focus to JTextField within JList

    Hey everybody, just joined the forums! I hope that I can learn a lot here and eventually contribute.

    I am making a GUI that displays various lists of strings that need to be edited easily. For each list I am using a JList and adding JTextFields using a custom cell renderer. However, I am having trouble giving focus to the JTextField when it is clicked; the JList takes focus instead and I can't type/modify the contents of the JTextArea. I tried to remedy this by placing a requestFocusInWindow() call within the renderer, but no luck. I believe that the containing JList is given focus by Swing after I call requestFocusInWindow(), but the problem may be elsewhere. Either way, I am not sure how to solve this!

    Java Code:
    public class CommandPanel extends JPanel {
    	JScrollPane m_commandScrollPane;
    	JList m_commandList;
    	CommandPanel() {
    		m_commandScrollPane = new JScrollPane();
    		m_commandScrollPane.setPreferredSize(new Dimension(Editor.INFO_PANEL_WIDTH,100));
    		m_commandList = new JList();
    		m_commandList.setCellRenderer(new CommandRenderer());
    	public void paintComponent(Graphics gr) {
    		Node n = Engine.getNode(Editor.getInstance().getVNPanel().getSelectedNode());
    		if (n != null) {
    	class CommandRenderer extends JTextField implements ListCellRenderer {
    		public CommandRenderer() {}
    		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    			Command c = (Command)value;
    			if (isSelected)
    			return this;	

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Give Focus to JTextField within JList

    The contents of a JList aren't editable -- use a single column JTable if you need an editable list.

    Also note that a renderer is used for painting; it isn't added to any component hierarchy and hence can never receive input events from mouse or keyboard. Read through the Oracle tutorial linked from the API of JTable for more detains on editor and renderer concepts.

    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sycosquirl18 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Re: Give Focus to JTextField within JList

    A JTable looks like it will be a much better option, thank you.

Similar Threads

  1. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  2. jList and jTextField
    By skidrow in forum NetBeans
    Replies: 8
    Last Post: 03-01-2011, 06:28 AM
  3. Replies: 9
    Last Post: 06-16-2010, 03:42 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts