-1

这是代码,我到目前为止,所以我想将选定的项目从一个列表移动到另一个,即使我不知道,就像在第二个列表中一样,我需要 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) 
  {
  }
});
4

1 回答 1

2

首先,您的第二个模型不需要一堆空元素(除非您打算允许使用将项目移动到特定位置)。

在您分配动作侦听器时,您将需要检查选择了哪些元素,然后简单地将其添加到您的第二个列表模型中。

使用JList#getSelectedValuesListList从第一个列表中获取一个选定的值。只需遍历列表并将每个元素添加到第二个列表。

根据您的需要,您可以从第一个列表中删除所选项目或提供检查以确定项目是否已存在于第二个列表中

于 2013-03-10T21:09:18.703 回答