How to delete items selected in a JList?

Ahoy,

I hava made a Multiple selection list (two list: one for

the items that might be selected and another one

for the selected items). How can I delete the (faultly) selected items in the second list?

Thanx,

// create function button

copy = new JButton( ">>" );

delete = new JButton ( "<<");

copy.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent e )

{

// place selected values in functionList

copyList.setListData(

functionList.getSelectedValues() );

}

}

);

delete.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent e )

{

// delete selected items from copyList

?

}

}

);

[831 byte] By [EviB] at [2007-9-26 1:33:40]
# 1

You must get the selected item and remove from the model...

delete.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int si = list.getSelectedIndex(); //get the index of the item selected

if (si > -1) {//to validate that be selected

listModel.removeElementAt(si); //remove from the listmodel

modifyTextField.setText("");

}

}

});

in this url you can see a complete sample..

http://www.esus.com/javaindex/j2se/jdk1.2/javaxswing/atomiccontrols/jlist/addtojlist.html

pedro.garcia at 2007-6-29 2:15:45 > top of Java-index,Archived Forums,Swing...