Results 1 to 6 of 6
  1. #1
    Nazerke is offline Member
    Join Date
    Feb 2011
    Location
    London
    Posts
    7
    Rep Power
    0

    Question cannot select from JList

    Java Code:
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.io.*;
    import java.util.logging.*;
    import javax.swing.*;
    
    public class NewMain extends JFrame {
    
        boolean action=false;
        JButton next;
        JList list;
       JScrollPane jScrollPane1;
       String a[];
       String b[];
        public NewMain()
        {
           b=new String[]{};
           jScrollPane1 = new JScrollPane();
           next = new JButton();
           setLayout(new FlowLayout());
    
            next.setText("next");
            next.addMouseListener(new java.awt.event.MouseAdapter() {
    	            public void mouseClicked(java.awt.event.MouseEvent evt) {
                    try {
                        buttonMouseClicked(evt);
                    } catch (IOException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
    	            }
    	        });
            add(next);
    
           list=new JList();
           add(list);
    	    }
    
    
         private void buttonMouseClicked(MouseEvent evt) throws IOException {
             action=true;
            }
    
         public void getArray()
        {
            int i=0;
            a=new String[]{"item1","item2","item3","item4"};
    
             do{
                list.setListData(a);
                if(action==true){
                        i++;
                  System.out.println(i+" "+list.getSelectedValue());
                        action=false;
                        continue;
                }
    
         }
    
         while(i<a.length);
         
         public static void main(String args[])
        {
         NewMain a = new NewMain();
    
         a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         a.setSize(500,500);
         a.setVisible(true);
         a.getArray();
    
    
         }
        }


    The question: cannot select fromJList, that is list.getSelectedValue() returns null
    Last edited by Fubarable; 02-19-2011 at 03:57 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may want to go through the Swing tutorials especially the section on using buttons (JButtons) and lists (JLists) as it will explain how to use these components.

    JButton Tutorial
    JList Tutorial

    Luck!

  3. #3
    riddhishah28 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Try with ActionPerformed Event on the list.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by riddhishah28 View Post
    Try with ActionPerformed Event on the list.
    Have you read the API documentation for the JList class? I guess not because you can't add ActionListeners to it; add a ListSelectionListener instead; please check your answer before you post it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    riddhishah28 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    i am new to Java so i just told to try..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by riddhishah28 View Post
    i am new to Java so i just told to try..
    Which is bad advice and doesn't tell the newbie that you haven't a clue about what you're recommending. Please "try" not to mislead the newbies if you're not sure of yourself. Bad advice is worse than no advice.

Similar Threads

  1. Replies: 2
    Last Post: 02-03-2011, 05:19 AM
  2. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  3. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  4. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  5. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 07:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •