Removing focus from JList

Posted on 2008-10-06
Last Modified: 2013-11-23
In the attached code, once a Jlist item is selected, it is not possible to remove focus from the JList even on clicking whitespace after the last item in the Jlist.
The possible alternative to get around this is to add a JLabel after the last JList item and this JLabel should cover till the end.
Is this a good alternative. If yes, please say how to implement this.
If there are better options, please say those also.
import java.awt.BorderLayout;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;

import javax.swing.Icon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JScrollPane;

import javax.swing.ListCellRenderer;

import javax.swing.UIManager;


public class MainClass {

  public static void main(String args[]) throws Exception {


    MainClass mc = new MainClass();

    Icon fileIcon = UIManager.getIcon("FileView.fileIcon");

	Icon directoryIcon = UIManager.getIcon("FileView.directoryIcon");

         Object elements[][] = { {"first.doc",fileIcon},


                                 {"third",fileIcon} ,







    JFrame frame = new JFrame("Trial");



    JList jlist = new JList(elements);

    ListCellRenderer renderer = new ComplexCellRenderer1();




    JScrollPane scrollPane = new JScrollPane(jlist);

    frame.add(scrollPane, BorderLayout.CENTER);


    frame.setSize(300, 200);





class ComplexCellRenderer1 implements ListCellRenderer {

  protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

  public Component getListCellRendererComponent(JList list, Object value, int index,

      boolean isSelected, boolean cellHasFocus) {

    Icon icon = null;

    String theText = null;

    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,

        isSelected, cellHasFocus);





    if (value instanceof Object[]) {

      Object values[] = (Object[]) value;

      theText = (String) values[0];

      icon = (Icon) values[1];

    } else {





    return renderer;



Open in new window

Question by:Sreejith22
  • 2

Accepted Solution

muktajindal earned 500 total points
ID: 22648815
You may attach a key listener ti the main frame so that the focus is reset whenever a key (say Esc) is pressed.

Author Comment

ID: 22648983
Hi expert. Thanks for the wonderful solution/advice.
I have found my alternative and it worked perfectly.
Please read and understand the question properly before answering. Anyway I award you points.
jlist.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {

                JList l = (JList)e.getSource();

                int i = l.locationToIndex(e.getPoint());

                Rectangle r = l.getCellBounds(i,i);

                if(!r.contains(e.getPoint())) {







Open in new window


Expert Comment

ID: 22649027
There is no compulsion as far as awarding the points is concerned. If you are not satisfied by the answer or the clue rovided by the answer, you may opt to keep the points with you !!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 81
unix in java example 9 55
Java SE 8u111  Lot of stuff broke 11 54
hibernate insert example 13 15
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now