Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

How to find which JList user clicked?

Thread view: 
yingjian.ma1955@gmail.com - 27 Apr 2006 06:09 GMT
Hi,

I have 2 JLists.  How can I find which JList user clicked?

Thanks.
Bart Cremers - 27 Apr 2006 06:17 GMT
JList list = (JList) MouseEvent.getSource()

This should give the JList instance.

Bart
Thomas Hawtin - 27 Apr 2006 11:25 GMT
> JList list = (JList) MouseEvent.getSource()
>
> This should give the JList instance.

Or just use two listeners...

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

yingjian.ma1955@gmail.com - 27 Apr 2006 17:39 GMT
Thank you for the msg. Below is the code.  It can move the names from
the left to the right if you click the name.  How can I move the name
from the right to left?  The listener is the class, so I am not sure
how to get 2 listeners.

import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;

public class List2 implements ListSelectionListener
{
JList l1,l2;
DefaultListModel model1 = new DefaultListModel();
DefaultListModel model2 = new DefaultListModel();
 public static void main(String[] args) {
   JFrame f = new JFrame("Lister v1.0");
   List2 l=new List2();
   f.setSize(200, 200);
   f.setLocation(200, 200);
    Container c = f.getContentPane();
    GridLayout grd = new GridLayout(1,2,10,10);
    c.setLayout(grd);
   l.create();
   c.add(l.l1);
    c.add(l.l2);
   f.setVisible(true);
     }

public void valueChanged(ListSelectionEvent e){
       Object selection = l1.getSelectedValue();
       if (l1.getSelectedIndex()>=0){
           model2.addElement(selection);
           model1.removeElementAt(l1.getSelectedIndex());
   }}

private void create(){
    l1 = new JList(model1);
    l2 = new JList(model2);
    model1.addElement("Mark");
    model1.addElement("Mary");
    model1.addElement("Mike");
    l1.addListSelectionListener(this);
 }}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.