Topic: 哪位有这样的现成控件

  Print this page

1.哪位有这样的现成控件 Copy to clipboard
Posted by: jfml
Posted on: 2003-06-04 11:50

左右两个JList或者JTable

中间是>> << > < 等选择按钮,全选,清除,单选,单个删除

把左边列表中的数据项选入右边的列表

哪位有这样的现成控件

share一下吧

谢谢

2.Re:哪位有这样的现成控件 [Re: CrazyJavar] Copy to clipboard
Posted by: Jove
Posted on: 2003-06-04 20:15

我自己写过一个,用JList,挺方便的,没几行
还是自己动手,丰衣足食 //虽然当初我也想找个现成的,嘻嘻

3.Re:哪位有这样的现成控件 [Re: Jove] Copy to clipboard
Posted by: Jove
Posted on: 2003-06-04 22:59

相关代码:
private JPanel createTargetPanel() {
JPanel p = new JPanel();
p.setBorder(new TitledBorder("测试的目标机器"));
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

//居中对齐: 一个是现有用户(包括Label和List), 一组按钮(>> <<),和一个测试用户
p.add(Box.createHorizontalStrut(50));
p.add(createUserList());
p.add(Box.createHorizontalStrut(5));
p.add(create2Button());
p.add(Box.createHorizontalStrut(5));
p.add(createTargetList());
p.add(Box.createHorizontalStrut(50));
return p;
}

private JPanel createUserList() {
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(150, Short.MAX_VALUE));
p.setLayout(new BorderLayout());
p.add(new JLabel("可选机器"), BorderLayout.NORTH);

int rowNo = onlineList.size();
Object[] users = new Object[rowNo];
Enumeration enum = onlineList.keys();
int i = 0;
while (enum.hasMoreElements()) {
users[i] = enum.nextElement();
i++;
}
listUser = new JList(users);
p.add(new JScrollPane(listUser), BorderLayout.CENTER);

return p;
}

private JPanel create2Button() {
JButton add, remove;

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(add = new JButton(">>"));
p.add(Box.createVerticalStrut(5));
p.add(remove = new JButton("<<"));

add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBetweenList(listUser, listTarget);
}
});
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBetweenList(listTarget, listUser);
}
});

return p;
}

private void moveBetweenList(JList listFrom, JList listTo) {
Object[] a = listFrom.getSelectedValues();

if (a.length == 0) {
return;
}

ListModel modelTarget = listTo.getModel();
LinkedList linkedlist = new LinkedList();
for (int i = 0; i < modelTarget.getSize(); i++) {
linkedlist.add(modelTarget.getElementAt(i));
}
for (int i = 0; i < a.length; i++) {
linkedlist.add(a[i]);
}
Collections.sort(linkedlist, new IPComparator());
listTo.setListData(linkedlist.toArray());

//删除listUser中的选中的项
int[] indices = listFrom.getSelectedIndices();

Vector vectorFrom = new Vector();
ListModel modelFrom = listFrom.getModel();
for (int i = 0, j = 0; i < modelFrom.getSize(); i++) {
//遍历List的每个项目
if (i != indices[j]) {
String str = (String) (modelFrom.getElementAt(i));
vectorFrom.add(str);
continue;
}
if (j < indices.length - 1) {
j++;
}
}
listFrom.setListData(vectorFrom);
}

private JPanel createTargetList() {
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(150, Short.MAX_VALUE));
p.setLayout(new BorderLayout());
p.add(new JLabel("目标机器"), BorderLayout.NORTH);
listTarget = new JList();
p.add(new JScrollPane(listTarget), BorderLayout.CENTER);

return p;
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923