Solved

Removing focus from JList

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
null output 3 42
jmss example java 2 31
web project error add remove 1 49
Java class and jar 3 41
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

740 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