[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to manipulate data from a JList component

Posted on 2004-08-10
6
Medium Priority
?
351 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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