Solved

Removing focus from JList

Posted on 2008-10-06
3
1,186 Views
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.
Regards,
Sreejith
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},
                                 {"second",fileIcon},      
                                 {"third",fileIcon} ,
                                 {"nineth",directoryIcon},
                                 {"eighth",directoryIcon},
                                 {"fifth",fileIcon}
          };
          
 
    JFrame frame = new JFrame("Trial");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    JList jlist = new JList(elements);
    ListCellRenderer renderer = new ComplexCellRenderer1();
    jlist.setCellRenderer(renderer);
    jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    jlist.setVisibleRowCount(-1);
    JScrollPane scrollPane = new JScrollPane(jlist);
    frame.add(scrollPane, BorderLayout.CENTER);
 
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}
 
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);
    renderer.setVerticalTextPosition(JLabel.BOTTOM);
    renderer.setHorizontalTextPosition(JLabel.CENTER);
 
 
    if (value instanceof Object[]) {
      Object values[] = (Object[]) value;
      theText = (String) values[0];
      icon = (Icon) values[1];
    } else {
      
    }
    renderer.setText(theText);
    renderer.setIcon(icon);
    return renderer;
  }
}

Open in new window

0
Comment
Question by:Sreejith22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
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.
0
 

Author Comment

by:Sreejith22
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())) {
                    l.getSelectionModel().setLeadSelectionIndex(l.getModel().getSize());
                    //l.getSelectionModel().setLeadSelectionIndex(-1);
                    l.clearSelection();
                }
            }
        });

Open in new window

0
 
LVL 5

Expert Comment

by:muktajindal
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 !!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to configure empty element in XML Document parser? 15 59
running on tomcat not jboss eap 7.0 3 55
Bitwise and to sum elements 2 54
Is there a Google Voice API? 9 74
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

752 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