Solved

How to manipulate data from a JList component

Posted on 2004-08-10
6
303 Views
Last Modified: 2012-05-05
Hi all,

I have a UI with two multi selection JList components, side by side.  When the user presses a button, I want the currently selected list items to be removed from one list, and placed in the other list.

It seems I am missing something however, since I can only see functions to populate the whole list, not add or remove items.  Here's my code so far:

  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if(source == addButton) {
      Object[] selectedItems = (Object[])list1.getSelectedValues();
      list2.setListData(selectedItems);  // need to replace this
    }
  }

I want to replace the last line with functionality to remove the selectedItems from list1 and place them in list2.  Can anyone help?
0
Comment
Question by:Dawkins
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 11763771
You need to use a DefaultListModel;

  DefaultListModel leftModel = new DefaultListModel() ;
  DefaultListModel rightModel = new DefaultListModel() ;

Then set the models into the lists:

   leftList.setModel( leftModel ) ;
   rightList.setModel( rightModel ) ;

then move the elements between the models, and the lists will update :-)

Tim
0
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 11763777
The best way to do this is to use a ListModel (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ListModel.html). If you implement a ListModel, you can use it as the underlying implementation of the list. Or you can use a DefaultListModel (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/DefaultListModel.html). That one will probably give you as much functionality as you need. You can pass the model in through the JList constructor.
0
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 11763784
Tim, you type to fast. =)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
ID: 11763803
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11763811
>  Tim, you type to fast. =)

Hehehe :-D
0
 

Author Comment

by:Dawkins
ID: 11764389
Ah I thought I needed to use ListModels but couldn't quite get how!  Thanks :)

>  Tim, you type to fast. =)

he has to to have a chance of beating Objects and CEHJ  ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now