没必要这么麻烦,只要一个actionlistener就行了。
Object [] object = jlist1.getSelectedValues();
DefaultMListModel mode = (DefaultListModel)jlist1.getModel();
for (int i.....)
model.removeeme.....(object[i])
model = (DefaultListeModlele)jlist2.getModel();
for (.....
)model .addeme....(object[i]
)
lt论坛真掉,不能删除输错的
Object [] object = jlist1.getSelectedValues();
DefaultMListModel mode = (DefaultListModel)jlist1.getModel();
for (int i.....)
model.removeeme.....(object[i])
model = (DefaultListeModlele)jlist2.getModel();
for (.....
)model .addeme....(object[i]
)
lt论坛真掉,不能删除输错的
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.DefaultListModel;//由于ListSelectionEvent是swing的事件,不是awt的事件,因此我们必须import
//javax.swing.event.*。public class Work_one implements ListSelectionListener,ActionListener
{
JList list1 = null;
JList list2 = null;
JLabel label = null;
JButton b1 = null;
JPanel buttonPanel = null;
String[] s = {"美国","日本","大陆","英国","法国","意大利","澳洲","韩国"}; public Work_one()
{
JFrame f = new JFrame("JList");
Container contentPane = f.getContentPane();
contentPane.setLayout(new BorderLayout());
b1 = new JButton("->");
b1.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(b1);
label = new JLabel();
DefaultListModel listModel = new DefaultListModel();
list1 = new JList(listModel);
for(int i = 0 ; i < s.length ; i++)
{
listModel.addElement(s[i]);
}
list1.setBorder(BorderFactory.createTitledBorder("您最喜欢到哪个国家玩呢?"));
list1.addListSelectionListener(this);list1.setSize(500,200); list2 = new JList(new DefaultListModel());
list2.setBorder(BorderFactory.createTitledBorder("222"));
list2.addListSelectionListener(this); contentPane.add(buttonPanel,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(new JScrollPane(list1),BorderLayout.WEST);
contentPane.add(new JScrollPane(list2),BorderLayout.EAST);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String args[])
{
new Work_one();
} public void valueChanged(ListSelectionEvent e)
{
int tmp = 0;
String stmp = "您目前选取:";
int[] index = list1.getSelectedIndices();//利用JList类所提供的getSelectedIndices()方法可得到用户所选取的所有
for(int i=0; i < index.length ; i++)//index值,这些index值由一个int array返回.
{
tmp = index[i];
stmp = stmp+s[tmp]+" ";
}
label.setText(stmp);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
if(list1.getSelectedIndices().length > 0)
{
int[] index = list1.getSelectedIndices();
DefaultListModel model = (DefaultListModel) list2.getModel();
DefaultListModel model1 = (DefaultListModel) list1.getModel();
String[] delete = new String[index.length]; for(int i = 0 ; i < index.length ; i++)
{
String temp = model1.elementAt(index[i]).toString();
model.addElement(temp);
delete[i] = temp;
}
for(int i = 0 ; i < index.length ; i++)
{
model1.removeElement(delete[i]);
}
list2.setModel(model);
list1.setModel(model1);
}
}
}
源码如下:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.WindowConstants;/*
* Created on 2004-7-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author Harper [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class listForAll { public static void main(String[] args) {
listform f=new listform("CountryChoose");
f.setSize(500,400);
f.setLocation(200,200);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/*
* Created on 2004-7-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author Harper [email protected]
* 这里只考虑了向右添加的情况;向左添加可以类推;
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/public class listform extends JFrame implements ListSelectionListener,ActionListener
{
JList list_one,list_two;
JButton button=new JButton("->");
JLabel label1,label2;
ArrayList one=new ArrayList();
ArrayList two=new ArrayList();
JScrollPane scroll_one;
JScrollPane scroll_two;
String tmp;
Object choose[];
Container c=getContentPane();
public listform(String title)
{
super(title);
c.setLayout(new FlowLayout());
one.add("-----请选择-----");
one.add("中国");
one.add("美国");
one.add("日本");
one.add("韩国");
one.add("台湾");
one.add("新加坡");
one.add("香港");
one.add("马来西亚");
one.add("法国");
one.add("德国");
one.add("英国");
one.add("瑞士");
one.add("瑞典");
two.add("-----已选择-----");
c.add(new JLabel(""));
c.add(new JLabel(""));
list_one=new JList(one.toArray());
list_two=new JList(two.toArray());
list_one.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//只能单选
label1= new JLabel("你想去哪?请选择!");
label2=new JLabel("你选择了:");
scroll_one=new JScrollPane(list_one);
scroll_one.setColumnHeaderView(label1);
scroll_two=new JScrollPane(list_two);
scroll_two.setColumnHeaderView(label2);
c.add(scroll_one);
c.add(button);
c.add(scroll_two);
button.addActionListener(this);
list_one.addListSelectionListener(this);
//可以添加list_two列表事件 ,用于返回不想选中的选项
} /* (non-Javadoc)
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
*/
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
choose=list_one.getSelectedValues();
for(int i=0;i<choose.length;i++)
{
one.remove(choose[i]);
} } /* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
for(int i=0;i<choose.length;i++)
{
two.add(choose[i]);
}
renew();
}
public void renew()
{
list_one.removeAll();
list_two.removeAll();
list_one.setListData(one.toArray());
list_two.setListData(two.toArray());
}}