主题:jlist
songwenjie
[专家分:260] 发布于 2008-05-04 15:44:00
怎样将两个JLIST的值进行移动
回复列表 (共1个回复)
沙发
happyboy2007 [专家分:3900] 发布于 2008-05-05 12:42:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class ListDemo extends JFrame implements ActionListener
{
private DefaultListModel dlm2 = new DefaultListModel();
private JList list2 = new JList(dlm2);
private DefaultListModel dlm1 = new DefaultListModel();
private JList list1 = new JList(dlm1);
private JButton but_in = new JButton();
private JButton but_inall = new JButton();
private JButton but_out = new JButton();
private JButton but_outall = new JButton();
/**
*
*/
private static final long serialVersionUID = 2715455060585849583L;
/**
* @param args
*/
public ListDemo()
{
getContentPane().setLayout(null);
final JScrollPane sp_1 = new JScrollPane();
sp_1.setBounds(21, 21, 119, 230);
getContentPane().add(sp_1);
sp_1.setViewportView(list1);
final JScrollPane sp_2 = new JScrollPane();
sp_2.setBounds(253, 21, 111, 230);
getContentPane().add(sp_2);
sp_2.setViewportView(list2);
but_in.setText("选入");
but_in.setBounds(146, 41, 101, 25);
getContentPane().add(but_in);
but_inall.setText("全部选入");
but_inall.setBounds(146, 72, 101, 25);
getContentPane().add(but_inall);
but_out.setText("选出");
but_out.setBounds(146, 184, 101, 25);
getContentPane().add(but_out);
but_outall.setText("全部选出");
but_outall.setBounds(146, 215, 101, 25);
getContentPane().add(but_outall);
this.fillList1();
but_in.addActionListener(this);
but_inall.addActionListener(this);
but_out.addActionListener(this);
but_outall.addActionListener(this);
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void fillList1()
{
for(int i=0;i<10;i++)
{
dlm1.addElement(String.valueOf(i));
}
}
public static void main(String[] args)
{
new ListDemo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but_in)
{
Object [] data = list1.getSelectedValues();
for(int i=0;i<data.length;i++)
{
dlm1.removeElement(data[i]);
dlm2.addElement(data[i]);
}
}
else if(e.getSource()==but_inall)
{
for(int i=0;i<dlm1.getSize();i++)
{
Object data = dlm1.getElementAt(i);
dlm2.addElement(data);
}
dlm1.removeAllElements();
}
else if(e.getSource()==but_out)
{
Object [] data = list2.getSelectedValues();
for(int i=0;i<data.length;i++)
{
dlm2.removeElement(data[i]);
dlm1.addElement(data[i]);
}
}
else if(e.getSource()==but_outall)
{
for(int i=0;i<dlm2.getSize();i++)
{
Object data = dlm2.getElementAt(i);
dlm1.addElement(data);
}
dlm2.removeAllElements();
}
}
}
我来回复