Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to swap info between JList's? 1

Status
Not open for further replies.

Salo

Programmer
May 25, 2001
6
SE
Hi,

I have a problem that I hope someone have some kind of tip of how to solve.

I have to JList's where one is consisting of a list of items and the other is empty (at fisrt). I also have two buttons which is used to move items between the lists.

I have solved how to copy an item (or more) from the first list to the second, but the problem is that I want to remove the items that were copied from the first list. How do I accomplish that?

Regards,
Salo
 
Just write an action listener for the second button as you did for the first - but this time you'll have to use the getSelectedItems() methods from the second list. You won't have to save the items you delete somewhere (I think) so this case should even be easier than the one you solved already...

Good Luck! If this does not solve your problem, try to post the critical part of your code... allow thyself to be the spark that lights the fire
 
Maybe I have to be more precise... When copying items from list #1 to #2 the items copied should disappear from list #1 and appear in list #2.
 
Ah, OK, sorry - in this case you'll have to define your own ListModel - just implement the interface and pass it in the constructor of the JList. You can then access it via list.getModel(), cast it and then call the remove(int[] index) method you created in your ListModel implementation.

Take care to remove the items in the correct order (top to bottom, the highest index first and the lowest last) because otherwise the indices get messed up... :-( allow thyself to be the spark that lights the fire
 
Alright, I have the code of the model:

Code:
import javax.swing.AbstractListModel;
import java.util.Vector;
import java.util.Arrays;

public class MyListModel extends AbstractListModel
{
  private Vector items;

  public MyListModel(Vector items)
  {
    this.items = items;
  }

  public int getSize()
  {
    return items.size();
  }

  public Object getElementAt(int index)
  {
    return items.get(index);
  }

  public void addElement(Object element)
  {
    items.add(element);
  }

  public void removeElements(int[] indices)
  {
    Arrays.sort(indices);
    for (int i = 1; i <= indices.length; i++)
    {
      items.remove(indices[indices.length - i]);
    }
  }
}
allow thyself to be the spark that lights the fire
 
There is actually an OK drag and drop (between JLists) on the sun website, which works OK, thing is i can't remember the URL. It is limited to text only and you will have to modify it yourself if you want to dnd other stuff (e.g. JComponents), but is worth a look.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top