Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Moving items up and down in a JList

807603Feb 22 2008 — edited Feb 23 2008
Hi-
I have a problem with moving selected items up and down in a JList..
When I select an item in the JList and press ctrl + (the up or down arrow key),
I want the selected item to move up (or down).
My code works. That is to say it does what I want, only the highlighted item does not correspond to the selected item.
When I move an item up I would like it to remain selected, which I guess it is as repeated moves will move the item.
But it does not appear to be working. because the highlight is above the selected item when moving up and below the selected item when moving down.
Here is my code:
public class MyClass  implements MouseListener,KeyListener
{
private JList linkbox;
private DefaultListModel dlm;
public List<String> Links= new ArrayList<String>();
private String Path;
private JScrollPane Panel;

...
...
    public MyClass  (String path,JScrollPane panel){
    super();
        Path=path;
        Panel=panel;
//Populate the ArrayList "Links" etcetera.
	dlm = new DefaultListModel();
        linkbox = new JList(dlm);
        for(int k=0;k<Links.size();k++){
            dlm.addElement(Links.get(k));
        }
...
...
...
    public void keyPressed(KeyEvent e) {
    System.out.println("You pressed "+e.getKeyCode());
       if(e.getKeyCode()==38&&e.getModifiers()==KeyEvent.CTRL_MASK){
        Link sellink =linkbox.getSelectedValue();
            MoveUP(sellink);
        }
        if(e.getKeyCode()==40&&e.getModifiers()==KeyEvent.CTRL_MASK){
            Link sellink =get(linkbox.getSelectedValue();
            MoveDown(sellink);
        }


    }
private void MoveUP(Link link){
    int pos=-1;
    for(int p=0;p<Links.size();p++){
        Link l=Links.get(p);
        if(l.indexOf(link)==0){
        pos=p;
        break;
        }
    }
    if(pos!=-1&&pos>0){
        Links.remove(link);
        Links.add(pos-1,link);
        dlm.removeAllElements();
        for(int k=0;k<Links.size();k++){
        dlm.addElement(Links.get(k));
        }
        Panel.setViewportView(linkbox);
        linkbox.setSelectedIndex(pos-1);

    }
}
    private void MoveDown(Link link){
        int pos=-1;
        for(int p=0;p<Links.size();p++){
            Link l=Links.get(p);
            if(l.indexOf(link)==0){
            pos=p;
            break;
            }
        }
        if(pos!=Links.size()-1&&pos>0){
        System.out.println("pos= "+pos);
            Links.remove(link);
            Links.add(pos+1,link);
            dlm.removeAllElements();
            for(int k=0;k<Links.size();k++){
            dlm.addElement(Links.get(k));
            }
            Panel.setViewportView(linkbox);
            linkbox.setSelectedIndex(pos+1);

        }
    }
Can anyone explain to me why this is so and if it can be fixed?
-Reg.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2008
Added on Feb 22 2008
12 comments
2,215 views