Solved

How to manipulate data from a JList component

Posted on 2004-08-10
6
330 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 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
Industry Leaders: 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: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 81
How do I remove an object from a 3 53
Why doesn't this text field show up on my Applet frame? 2 28
jsp error 6 47
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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