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
Solved

Removing focus from JList

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

829 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