Results 1 to 8 of 8
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Operations on JList

    Hie guys, can I please have some help.

    I have a JList with 20 items in the list. I want to run certain code depending on what option is selected. How can I identify which item has been selected?

    Thank you in advance for your help.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Have you looked in API this class?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Yes I have read the API but I still have no idea as to how I can implement what I want. I am using netbeans.

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    see JList (Java Platform SE 6))

    PS. I advice you to write a code. which Independence from your IDE. I mentioned this, because many people don't know as they can run your code without IDE.
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    mubbashar is offline Member
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1
    Rep Power
    0

    Default

    you have to implement the listener on JList

    e.g
    you have a JList with name myjList then you can implement mouseClick listener like this


    myjList.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    JList dList = (JList) evt.getSource();
    int selected=dList.getSelectedIndex();
    }
    });

    after getting selected index you can perform you logic

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    Yes I have read the API but I still have no idea as to how I can implement what I want. I am using netbeans.
    What's the first method listed for JList?

    db

  7. #7
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    What's the first method listed for JList?

    db
    addListSelectionListener

    I think I know what the problem is here. If you are experienced with Java you can easily navigate the API, understand what it says and get back to work. Good.

    But if you are a novice, like I am, the API will make your eyes water :(

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    There's a link to the tutorial on How to Use Lists in the JList API. Here's another link:
    How to Write a List Selection Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    Bookmark this one:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    You'll find many answers to your problems there.

    db
    Last edited by DarrylBurke; 03-22-2011 at 01:14 AM.

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  3. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  4. Date operations
    By SAR in forum New To Java
    Replies: 9
    Last Post: 07-21-2010, 09:13 AM
  5. String operations..
    By sireesha in forum New To Java
    Replies: 4
    Last Post: 12-14-2007, 02:04 AM

Posting Permissions

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