?
Solved

How to manipulate data from a JList component

Posted on 2004-08-10
6
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 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