import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class demoJList implements ActionListener{
JFrame mainJFrame;
Container con;
JButton addBtn,delBtn;
JList orignList,destList;
DefaultListModel orignModel,destModel;
JSplitPane baseSplitPane;
JScrollPane leftJSPane,rightJSPane;
JPanel pane;
static final String msg[] = {"北京","上海","天津","遼寧","吉林","四川","湖南","湖北","廣東"};
public demoJList(){
mainJFrame = new JFrame("JList 示例");
con = mainJFrame.getContentPane();
con.setLayout(new FlowLayout());
orignModel = new DefaultListModel();
for(int i = 0;i < msg.length;i++){
orignModel.addElement(msg[i]);
}
orignList = new JList(msg);
destModel = new DefaultListModel();
destList = new JList(destModel);
leftJSPane = new JScrollPane(orignList);
rightJSPane = new JScrollPane(destList);
baseSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,leftJSPane,rightJSPane);
con.add(baseSplitPane,BorderLayout.CENTER);
addBtn = new JButton("選中>>");
delBtn = new JButton("撤銷<<");
pane = new JPanel();
pane.add(addBtn);
pane.add(delBtn);
addBtn.addActionListener(this);
delBtn.addActionListener(this);
con.add(pane,BorderLayout.SOUTH);
mainJFrame.setSize(300,300);
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
baseSplitPane.setDividerLocation(0.5);
}
public void actionPerformed(ActionEvent e){
int i;
if(e.getSource() == delBtn){
for(i =0;i < destModel.getSize();i++ ){
if(destList.isSelectedIndex(i)){
orignModel.addElement(destModel.elementAt(i));
}
}
for(i--;i>=0;i--){
if(destList.isSelectedIndex(i))
destModel.removeElementAt(i);
}
}
else if(e.getSource() == addBtn){
for(i =0;i < orignModel.getSize();i++ ){
if(orignList.isSelectedIndex(i)){
destModel.addElement(orignModel.elementAt(i));
}
}
for(i--;i>=0;i--){
if(orignList.isSelectedIndex(i))
orignModel.removeElementAt(i);
}
}
}
public static void main(String args[]){
new demoJList();
}
}