DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005
    Posts
    5

    JList returns null or -1

    Hi Guys,

    I have a JList that I want to double click and get the String that is currently selected.However, the .getSelectedValue.toString keeps returning null or -1 even when I have a String selected.

    My code is as follows:

    Code:
        public void makeLstChatters(){
            //make the listener for the JList 
            MouseListener mouseListener = new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {  
                        try{
                            System.out.println(lstChatters.getSelectedValue().toString());
                            String str = lstChatters.getSelectedValue().toString();
                            serv.whisper(str, txtAreaMessage.getText());
                        }
                        catch(Exception ex){
                            ex.printStackTrace();
                        }
                  }
                }
            };
            
            listModel = new DefaultListModel();
    
            lstChatters = new JList(listModel);
    
            JScrollPane scrollPane = new JScrollPane(lstChatters);
            lstChatters.addMouseListener(mouseListener);
    can anyone see where I am going wrong?

    Thanks a million!!

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    if (e.getClickCount() == 2) {
    int index = list.locationToIndex(e.getPoint());
    System.out.println("Double clicked on Item " + index);
    }

    then to get the string clicked...

    listModel.get(index)

  3. #3
    Join Date
    Nov 2005
    Posts
    5
    excellent. thats worked a treat.

    Thanks a million!!

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 09:09 AM
  2. Getting a large number of identities
    By Colin McGuigan in forum Database
    Replies: 12
    Last Post: 04-15-2002, 07:43 AM
  3. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM
  4. Replies: 1
    Last Post: 07-12-2001, 06:15 PM
  5. Multi-row calculations
    By Bob Hines in forum Database
    Replies: 7
    Last Post: 04-27-2000, 11:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links