Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Right Click and Select an item in Jlist

Posted on 2008-09-29
5
Medium Priority
?
2,009 Views
Last Modified: 2013-11-23
Hi,
In the attached code, if I 'Ctrl+left click' and select few items and I right click on top of another item, how can I make the right clicked item selected and the older ones de-selected.
Also, if I 'Ctrl+left click' and select few items and I right click on top of any of these selected items, the selection has to be there on these items(I am trying to implement something similar to file/folder selection in windows -- adding popup menu will be the next step I do).
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
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
arioh earned 2000 total points
ID: 22603183
   jlist.addMouseListener( new MouseAdapter(){
      public void mouseClicked( MouseEvent Evt ) {
        if ( Evt.getButton() == MouseEvent.BUTTON3 ) {
          int Idx = jlist.locationToIndex( Evt.getPoint() );
          if ( Idx >= 0 ) {
            Rectangle Rect = jlist.getCellBounds( Idx, Idx );
            Idx = Rect.contains( Evt.getPoint().x, Evt.getPoint().y ) ? Idx : -1;
          }
          if ( Idx < 0 || !jlist.getSelectionModel().isSelectedIndex( Idx ) ) {
            jlist.getSelectionModel().clearSelection();
            if ( Idx >= 0 )
              jlist.getSelectionModel().setSelectionInterval( Idx, Idx );
          }
        }
      }
    } );
0
 

Author Closing Comment

by:Sreejith22
ID: 31501466
Thanks alot. The full points are for you.
0
 

Author Comment

by:Sreejith22
ID: 22603217
Thanks arioh
0
 

Author Comment

by:Sreejith22
ID: 22603325
The mouse event 'Evt' returns false for popupMenu.isPopupTrigger(Evt) . So I am not able to generate popup menu.
Please help
0
 
LVL 9

Expert Comment

by:arioh
ID: 22603434
check for isPopupTrigger() in mouseReleased method

      public void mouseReleased( MouseEvent Evt ) {
        if ( Evt.isPopupTrigger() ) {
          System.out.println( "popup" );
        }
      }

from the JDK API:
Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

972 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