SWT问题:table自动逐行选中的问题

甜甜小米粒 2011-12-21 02:25:46
我用SWT的table控件做了一个表格,想实现的效果是:点击一个按钮,能够自动逐行选中table的每一行,现在的问题是:我点击按钮之后,逐行选中的效果出不来,直接到了最后一行,代码如下,请高手帮忙看一下:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;


public class OperationUI extends Shell {
public Table table;
SenderDriver thread = null;
public TableItem[] ti;
public String dirName = "";
public static OperationUI opt;

/**
* Launch the application.
*
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
OperationUI shell = new OperationUI(display);
opt = shell;
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the shell.
*
* @param display
*/
public OperationUI(final Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FormLayout());
Group group_2 = new Group(this, SWT.NONE);
group_2.setText("报文列表");
group_2.setLayout(new FormLayout());
FormData fd_group_2 = new FormData();
fd_group_2.left = new FormAttachment(0, 6);
fd_group_2.top = new FormAttachment(0, 10);
fd_group_2.bottom = new FormAttachment(100, -10);
group_2.setLayoutData(fd_group_2);
ScrolledComposite scrolledComposite = new ScrolledComposite(group_2,
SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
FormData fd_scrolledComposite = new FormData();
fd_scrolledComposite.bottom = new FormAttachment(100, -70);
fd_scrolledComposite.top = new FormAttachment(0, 10);
fd_scrolledComposite.left = new FormAttachment(0);
fd_scrolledComposite.right = new FormAttachment(100, -7);
scrolledComposite.setLayoutData(fd_scrolledComposite);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
table = new Table(scrolledComposite, SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnDir = new TableColumn(table, SWT.NONE);
tblclmnDir.setWidth(80);
tblclmnDir.setText("目录");
TableColumn tblclmnFilename = new TableColumn(table, SWT.NONE);
tblclmnFilename.setWidth(87);
tblclmnFilename.setText("报文");
TableColumn tblclmnReturnfile = new TableColumn(table, SWT.NONE);
tblclmnReturnfile.setWidth(164);
tblclmnReturnfile.setText("返回结果");
TableColumn tblclmnModify = new TableColumn(table, SWT.NONE);
tblclmnModify.setWidth(85);
tblclmnModify.setText("是否修改过");
TableItem tabItem1 = new TableItem(table,
SWT.Selection | SWT.ARROW_DOWN);
tabItem1.setText(new String[] {"test1","test2","test3","test4"});
TableItem tabItem2 = new TableItem(table,
SWT.Selection | SWT.ARROW_DOWN);
tabItem2.setText(new String[] {"test1","test2","test3","test4"});
TableItem tabItem3 = new TableItem(table,
SWT.Selection | SWT.ARROW_DOWN);
tabItem3.setText(new String[] {"test1","test2","test3","test4"});
scrolledComposite.setContent(table);
TableItem tabItem4 = new TableItem(table,
SWT.Selection | SWT.ARROW_DOWN);
tabItem4.setText(new String[] {"test1","test2","test3","test4"});
TableItem tabItem5 = new TableItem(table,
SWT.Selection | SWT.ARROW_DOWN);
tabItem5.setText(new String[] {"test1","test2","test3","test4"});
scrolledComposite.setMinSize(table
.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button button = new Button(group_2, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

for (int i = 0; i < table.getItemCount(); i++) {
table.setSelection(i);

try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


}
});
FormData fd_button = new FormData();
fd_button.top = new FormAttachment(scrolledComposite, 6);
fd_button.left = new FormAttachment(scrolledComposite, 10, SWT.LEFT);
button.setLayoutData(fd_button);
button.setText("发送");
createContents();
}

/**
* Create contents of the shell.
*/
protected void createContents() {
setText("报文操作");
setSize(495, 467);

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
...全文
134 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
甜甜小米粒 2011-12-22
  • 打赏
  • 举报
回复
没人理吗?哎

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧