SWT Table中 嵌入RadioButton

  每行一个User,在team中选择一个。

 

  本来想每行一个Group,每个Cell中一个RadioButton,可以实现,但是所有radio属于一个group,不能满足我的一行一个group,所以最终结果是,多个RadioButton集中在一个Cell里。

 

  截图见我的百度空间http://hi.baidu.com/lylegend13/blog/item/d17c3b34fa797b2f5ab5f529.html?timeStamp=1299054794488

 

		viewer = new TableViewer(container, SWT.FULL_SELECTION);

		table = viewer.getTable();
		table.setLinesVisible(true);

		new TableColumn(table, SWT.None).setWidth(cw);

		// column names
		if (colNames == null) {
			teamAmount = inputTeams.size();

			colNames = new String[teamAmount + 1];

			for (int i = 0; i < teamAmount; i++) {
				colNames[i] = inputTeams.get(i).getName();
			}
			colNames[teamAmount] = "no team";
		}

		// column
		TableItem col = new TableItem(table, SWT.None);
		col.setText(0, "");

		ch = col.getBounds().height;

		Group group = new Group(table, SWT.None);
		group.setBounds(col.getBounds().x + cw - 3, col.getBounds().y, cw
				* (teamAmount + 1), ch * 2 + 1);

		Button[] buttons = new Button[teamAmount + 1];
		for (int i = 0; i <= teamAmount; i++) {
			buttons[i] = new Button(group, SWT.None);
			buttons[i].setText(colNames[i]);
			buttons[i].setBounds(i * cw, 0, cw, ch * 2 + 1);
			buttons[i].addSelectionListener(new colSelectionListener(i));
		}
		buttons[teamAmount].setText("no team");

		TableEditor editor = new TableEditor(table);
		editor.setEditor(group, col, 1);
		editor.grabHorizontal = true;// have to

		// no-use row
		new TableItem(table, SWT.None);

		// nodes
		for (UserNode node : inputNodes) {
			TableItem item = new TableItem(table, SWT.None);
			item.setText(0, node.getName());
			item.setData("node", node);
			item.setData("col", teamAmount);

			Group g = new Group(table, SWT.None);
			g.setBounds(item.getBounds().x + cw - 3, item.getBounds().y, cw
					* (teamAmount + 1), ch);
			Button[] b = new Button[teamAmount + 1];
			for (int i = 0; i <= teamAmount; i++) {
				b[i] = new Button(g, SWT.RADIO);
				b[i].setBounds(i * cw, 0, cw, ch);
				b[i].setBackground(table.getBackground());
				b[i].addSelectionListener(new nodeSelectionListener(item, i));
			}
			b[teamAmount].setText("no team");

			item.setData("group", g);

			editor.setEditor(g, item, 1);
		}

 

	class colSelectionListener implements SelectionListener {

		int index;

		public colSelectionListener(int index) {
			this.index = index;
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			widgetSelected(e);
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			TableItem[] items = table.getItems();
			for (int i = 1; i <= nodeAmount; i++) {
				TableItem item = items[i + 1];
				item.setData("col", index);
				for (int j = 0; j <= equAmount; j++) {
					((Button) ((Group) item.getData("group")).getChildren()[j])
							.setSelection(j == index ? true : false);
				}
				if (index < equAmount) {
					if (!selectedNodes.contains(item)) {
						selectedNodes.add(item);
					}
				} else {
					if (selectedNodes.contains(item)) {
						selectedNodes.remove(item);
					}
				}
			}
		}
	}
 

 

	class nodeSelectionListener implements SelectionListener {

		TableItem item;
		int index;

		public nodeSelectionListener(TableItem item, int index) {
			this.item = item;
			this.index = index;
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			widgetSelected(e);
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			item.setData("col", index);
			if (index == equAmount) {
				if (selectedNodes.contains(item)) {
					selectedNodes.remove(item);
				}
			} else {
				if (!selectedNodes.contains(item))
					selectedNodes.add(item);
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值