Solved

Removing focus from JList

Posted on 2008-10-06
3
1,207 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

623 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