如何将所选项目从一个默认列表移动到另一个默认列表

时间:2013-03-10 21:02:11

标签: java swing

这是代码,我到目前为止,所以我想将所选项目从一个列表移动到另一个列表,即使我不知道,就像在第二个列表中,我需要getElement或addelement ... 请帮助我..你可以解释一下,我能做些什么来将一个元素从一个元素移到另一个元素,举个例子

 //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) 
  {
  }
});

1 个答案:

答案 0 :(得分:2)

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

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

使用JList#getSelectedValuesList从第一个列表中获取List个所选值。只需在列表中进行交互,并将每个元素添加到第二个列表中。

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