Solved

Removing focus from JList

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

757 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

17 Experts available now in Live!

Get 1:1 Help Now