Solved

How to manipulate data from a JList component

Posted on 2004-08-10
6
290 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json format text only 4 65
groovy example issue 10 66
bitbucket vs gitbucket 3 32
Securing Jmx Console and web console 2 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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