Multiple selection in JList

How can I set up a JList that always uses multiple selection. That is, it acts as though the CTRL key is held down when the user selects items.

Here's an example of multi-selection in a listbox, but using the shift key
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;
public class Multi extends Applet implements KeyListener
     List list = new List(5, true);
     public void init()
          createGUI();
          setSize(300,300);
          this.setVisible(true);
     public void createGUI()
          for (int j = 0; j < 20; j++)
               list.addItem("TESTING MULTI-SELECTION IN A LIST");
          add (list);
          list.addKeyListener(this);
     public void keyReleased(KeyEvent e)
          int key = e.getKeyCode();
          if (key == KeyEvent.VK_SHIFT)
               int itm[] = list.getSelectedIndexes();
               for(int i = itm[0]; i < itm[1]; i++)
                    list.select(i);
     public void keyTyped(KeyEvent e){}
     public void keyPressed(KeyEvent e){}
}

Similar Messages

  • Multiple Selection with JList

    Hello!
    I have a Frame with a JList that needs to be able to do multiple slection. The JList ist filled with special names which are found when the user opens a certain file. (so a file-chooser is here too) When the User selects a name every paragraph containing that name should be posted in a textarea.
    However, when i run the program I can select multiple items, BUT it just recognises that the user selected one of the items, and I think it's the first one clicked.
    How can I change that? I have used getSelectedValues() but it just won't work...
    Can someone please help?
    Thanks

    So getSelectedValues() is only returning an array of size 1?
    Paul

  • Multiple selection using JLists

    (I put this question in the wrong forum, so there is a cross post but because it is in the wrong one)
    I have a JList that needs to allow for multiple slection of items, when i run the program I can select multiple items, BUT it only recognises that I have selected one of the items, the first one.
    Why is this and how can I store the selected Items in seperate strings so that I can use them.
    Any One know?
    Cheers

    Cross-post: http://forum.java.sun.com/thread.jspa?threadID=607480
    Again, please don't cross-post.
    Why shouldn't you cross-post? Well, look what happened yesterday: You posted the same message in the Swing forum, the Collections forum, and the Java Programming forum. Several people noticed about the same time, and now the thread in the Java Programming forum has a reply that points to the thread in the Collections forum, and the thread in the Collections forum has two replies that came about the same time, one pointing to the Swing forum and one pointing to the Java Programming forum. And people started to give you advice on how to solve your problem in both the Collections forum and in the Swing forum. This mess makes it very difficult for people to follow what is going on, and people will also spend unnecessary time posting a reply in one forum, where exactly the same thing already has been said by some other poster in another forum.

  • MULTIPLE SELECTION of Jlist only with mouse

    I want select many items of Jlist but I need make only with the mouse.
    I cann�t use keyboard.
    I don�t know how do it, because I selected
    listSelectionModel.MULTIPLE_INTERVAL_SELECTION
    but I need do a click in the first item and press key shif + click in the last item.
    but I must do a program only with the mouse.
    how I can make it?
    thanks

    hi,
    heres a quick and dirty version:
    set an ListSelectionModel at your List that overwrites the DefaultListSelectionModel class like this:
    ListSelectionModel selectionModel = new DefaultListSelectionModel() {
    int anchor = -1;
    public void setSelectionInterval(int index1, int index2) {
    if( index1 == index2 ){
    if( anchor = -1 ) anchor = index1;
    super.setSelectionInterval(anchor, index2);
    }else {
    super.setSelectionInterval(index1, index2);
    public void setValueIsAdjusting(boolean b) {
    super.setValueIsAdjusting(b);
    if(!b) anchor = -1;
    as long as single list entries should be selected, while the mouse is dragged ( valueIsAdjusting ), the entry is added to a selection range instead.
    I didnt compile the code, so maybe there are some syntax struggles.
    if you want to do it the hard and stoney way you have to implement a new ui class, that accomplish your needs.
    regards,
    stefan

  • Problem in multiple selections in a jList

    I have two jLists. Now I have to select some items from one jlist and have to transfer in the other one. But while trying to do it in the runtime I am getting some unexpected runtime errors including "Exception occurred during event dispatching". When I try to transfer only one item it works fine but shows problem in multiple selections. Pleast give your suggestions.

    [_First suggestion_|http://catb.org/~esr/faqs/smart-questions.html]
    [_Second suggestion_|http://mindprod.com/jgloss/sscce.html]
    db

  • Is it possible to diable multiple selections in JTable and JList?

    as the title states, is it possible to disable multiple selections? how to prevent user from using ctrl or shift keys to select multiple rows?

    Use the setSelectionMode(ListSelectionModel.SINGLE_SELECTION ) for your JList / JTable.

  • Multiple-selection JList

    here is my another problem:
    how to retrieve the data (tables, columns) from access database to the JList? i already success connected database to the jdbc-odbc bridge...after got the all column names (JList2) from selected table (JList1), i would like to select the certain column name by clicking the ( > , <, >>, << jbutton1, 2, 3, 4 ) to the another JList (JList3) because not all the column is useful to use to analyse. i think i need to use multiple selection JList but i don't know how to start because i am new to java.
    thx!

    Usually data from a database would be stored in a JTable as it already provide support for multiple rows and columns:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
    Also, with JDK1.3 there is some sample code of using a JTable with a database. I'm not sure if the code actually runs, but at least you can look at the source to see the steps involved:
    jdk1.3\demo\jfc\TableExample\src>

  • How to use JList's ensureIndexIsVisible and multiple selection using CTRL

    Hello everyone. I am stuck on how to use JList's ensureIndexIsVisible() method and to also allow multiple rows in the list to be selected. Currently, when I begin typing a number stored in the list (i.e. 10001) the focus will go to that row; however, when I press the CTRL key and begin typing another number (i.e. 30001) the focus never goes to the new row. I have a suspicion that I need to cancel out the CTRL key somehow when typing the numbers but also need to keep the knowledge that the CTRL key is pressed to allow for multiple selection. Thanks in advance for your help!
    Regards,
    Will

    Hello everyone. I am stuck on how to use JList's ensureIndexIsVisible() method and to also allow multiple rows in the list to be selected. Currently, when I begin typing a number stored in the list (i.e. 10001) the focus will go to that row; however, when I press the CTRL key and begin typing another number (i.e. 30001) the focus never goes to the new row. I have a suspicion that I need to cancel out the CTRL key somehow when typing the numbers but also need to keep the knowledge that the CTRL key is pressed to allow for multiple selection. Thanks in advance for your help!
    Regards,
    Will

  • Multiple Selection with JCombo Box

    I want to make a multiple - selection JComboBox enables the user to select many items from JComboBox and copy items from one JComboBox to another.How can I do this?

    Hello,
    ComboBoxModel allows only single selection. It's better to use a JList instead because it isnt worthwhile the effort to create another multi-selection model.
    regards,
    Tim

  • Multiple selection in a Combo Box of a JOptionPane.

    Hello,
    Can we have multiple selection of the items in a Combo Box of a JOptionPane?
    An example will be highly appreciated.
    Thanks.

    Use a JList for multiple selection.

  • List with multiple selection

    Hello;
    I seek a code to implement a list with multiple selection for a browser Web (HTML is imited).
    I have a list containing of the years, the user can select one or several years and I created dynamically a array : for each year a column.
    Thank you;

    Look at JList.
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html
    � {�                                                                                                                                                                           

  • Multiple selection in common dialog box

    Hi friends
    I have to select a multiple file from specified directory and disply it in image file, when i m using get_file_name function it allows me to select single file and read image file one by one but i want select not all some file from directory and whatsoever file is selected it will display it in image file.
    please suggest me.........

    Use a JList for multiple selection.

  • Is it possible to paste multiple selections from excel?

    How do you detect in Java whether a multiple selection in excel has been copied to the clipboard.
    For instance: I have an excel spreadsheet - 3 rows x 3 columns. I highlight the first 3 rows in column 1 and the first 3 rows in column 3 then I copy this to the clipboard (ctrl-c). If I now go to a text editor and paste this then the data from all 3 columns, not the expected 2 columns, appears.
    Likewise, in Java I have tried using the clipboard object looking at all the different data flavours to find a way of detecting that only data from the 1st and the 3rd columns were copied. But I can't find a way.
    This is most likely a microsoft issue in that they never make this information available from the clipboard.
    Can anyone shed any light on this one?

    Use the setSelectionMode(ListSelectionModel.SINGLE_SELECTION ) for your JList / JTable.

  • Managing multiple selections using listModel......

    Hey,
    Im a newbie to java can anyone give me a pointer or two as to how I would go about removing multiple selections from a JList using listModel??
    Any help would be great.
    -Ben

    Why not try using the removeSelectionInterval(int,int) method of the JList itself? It should be fairly obvious as to how to proceed with this (i.e. stepping through the list element at a time, deselecting as necessary).
    I'm watching this topic if you need further help!

  • Dropdowns with multiple selection functionality

    Hi Gurus,
    How can I achieve dropdowns with range and multiple selection functionality in WDA?
    Your help will be highly appreciated.
    Regards,
    David.

    Look at JList.
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html
    � {�                                                                                                                                                                           

Maybe you are looking for

  • Application installation issue

    I am trying to install an application called Teamsite on Solaris. I have successfully gunzipped the files to a directory 'Teamsite'. Now according to the installation manual, I need to run the following command to start the Teamsite installation: # .

  • Build a JTree from data

    Hello everyone, I have to build a JTree from a table data from database. The table structure is (id,parent_id,name). In order to build the tree, I have to the following 2 loop searching for the parent_id while (i<size){ while(j<size){ //find it's par

  • I can't find where to go to login to hide my purchases? HELP!

    I am trying to hide purchases (books, etc) from my account. I can't find where to do this at. Please help! Thanks!!

  • Sync between Curve 9300 and Win 7 Contacts

    I have this situation: in my BB there are 438 contacs and in Win 7 LIve Contacts there are 531 contacts.  If I change and/or insert new contact in BB the sync updated the Win 7 Contacts, but it is impossible to add the 93 contacts present in Win 7 to

  • Explorer crash on windows 8.1

    windows explorerkeeps crashing. I have set debug options to produce a dump as followed: Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Explorer.exe] "DumpFolder"=hex(2):43,00,3a