这是代码,我到目前为止,所以我想将选定的项目从一个列表移动到另一个,即使我不知道,就像在第二个列表中一样,我需要 getElement 或 addelement ...请帮助我..you可以通过举个例子向我解释一下,我该怎么做才能将一个元素从列表移动到另一个元素
//this is the code, just to the set the position
GridBagConstraints gridConstraints;
citiesLabel.setText("Destination City");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
// below code is also set the position of the list
gridConstraints.insets = new Insets(10, 0, 0, 0);
getContentPane().add(citiesLabel, gridConstraints);
citiesScrollPane.setPreferredSize(new Dimension(150, 100));
citiesScrollPane.setViewportView(citiesList);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(citiesScrollPane, gridConstraints);
**This is my first list , which have all the values , that i want to move**
final DefaultListModel citiesListModel = new DefaultListModel();
citiesListModel.addElement("San Diego");
citiesListModel.addElement("Los Angeles");
citiesListModel.addElement("Orange County");
citiesListModel.addElement("Ontario");
citiesListModel.addElement("Bakersfield");
citiesListModel.addElement("Oakland");
citiesListModel.addElement("Sacramento");
citiesListModel.addElement("San Jose");
citiesListModel.addElement("San Francisco");
citiesListModel.addElement("Eureka");
citiesListModel.addElement("Eugene");
citiesListModel.addElement("Portland");
citiesListModel.addElement("Spokane");
citiesListModel.addElement("Seattle");
citiesList.setModel(citiesListModel);
citiesList.setSelectedIndex(0);
// 此代码设置所选列表的位置
SecondLabel.setText("Selected Cities");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 0;
gridConstraints.insets = new Insets(10, 0, 0, 0);
getContentPane().add(SecondLabel, gridConstraints);
SecondScrollPane.setPreferredSize(new Dimension(150, 100));
SecondScrollPane.setViewportView(SecondList);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(SecondScrollPane, gridConstraints);
**This is my second list, which is empty, where i want to the moved value..**
DefaultListModel citiesListModel1 = new DefaultListModel();
citiesListModel1.addElement("aa");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
citiesListModel1.addElement("");
SecondList.setModel(citiesListModel1);
SecondList.setSelectedIndex(0);
**This is the button,for help me move selected city, from first list**
AssignButton.setText("Add Cities");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 2;
gridConstraints.insets = new Insets(0, 0, 10, 0);
getContentPane().add(AssignButton, gridConstraints);
AssignButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});