これはコードです, 私はこれまでのところ, 選択したアイテムをあるリストから別のリストに移動したいです. 2番目のリストのように, 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)
{
}
});