简体   繁体   中英

How can I simplify this module?

I have some method which looks awful(especially number of parameters). I wonder how can I make this code cleaner.

The method works with JLists and setting a new model( DefaultListModel ). So it just swaps items between two JLists and deletes swapped item in list where was the item taken.

Сriticism and advice are welcome.

Call the method example:

moveToOtherJList(newOrdersModel, newOrdersJList, inProcessOrdersModel, inProcessOrdersJList);

The method:

private void moveToOtherJList(DefaultListModel firstModel, JList firstJList, DefaultListModel secondModel,  JList secondJList)
{
    int selectedIndex = firstJList.getSelectedIndex();
    secondModel.addElement(firstJList.getSelectedValue());
    secondJList.setModel(secondModel);
    firstModel.remove(selectedIndex);
}

I have some method which looks awful(especially number of parameters).

Well there is no need to pass either ListModel, since you can get the ListModel from the JList.

So I would define the method as:

public void moveToOtherJList(JList fromJList, JList toJList)
{
    int selectedIndex = fromJList.getSelectedIndex();
    DefaultListModel fromModel = (DefaultListModel)fromJList.getModel();
    DefaultListModel toModel = (DefaultListModel)toJList.getModel();

    toModel.addElement(fromJList.getSelectedValue());
    fromModel.remove(selectedIndex);
}

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