简体   繁体   中英

Displaying selected items in one jlist to another in java

I am using 4 JList s that is an array,

JList jlst = new JList[4];

Then I am adding first list items from array of objects,

jlst[0].setListData(getObjAL());

Here the function getObjAL() function will give the array of objects.

I want to display selected item of first list( jlst[0] ) into second list( jlst[1] ).

For that I am writing the code,

jlstPrimitives[i].addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent listevt) {
        Jlist objLstTemp = (Jlist) listevt.getSource();
        jlst[1].setListData(objLstTemp.getSelectedValue()));
    }
});

But it is not displaying in list[1] . Please any one help me...

I can think of a dozen things that might be going wrong, none of which would affect you. For better support in the future, post a runnable example that demonstrates your problem. Pasting code out of context doesn't help (alot).

From your code, getObjAL() seems to be returning a Object[] array (single dimension), yet when you select a value, you seem to be assuming that the selected value is actually an array, which I'm pretty sure it isn't.

JList#setListData is expected either a object array ( Object[] ) or Vector .

Try something like this instead.

jlstPrimitives[i].addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent listevt) {
        Jlist objLstTemp = (Jlist) listevt.getSource();
        jlst[1].setListData(new Object[]{objLstTemp.getSelectedValue()}));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM