如何在动态生成的树项目上执行单个选择?
我需要执行以下任务: 在树查看器中选择单个树项目时,相关数据应该显示在表查看器中。 我使用 rs.getString() 方法从 MySQL 检索数据,并使用 setText() 方法将获得的字符串数组添加到树项中。 我需要选择动态生成的树查看器中显示的字符串值之一。
在对树使用选择事件时,所有生成的元素都会被选择。我怎样才能获得单一选择? 对于 TreeViewer 是否有类似于 getSelectedItem() 的方法? 这是我用来生成树项目的代码:
Tree tree_1 = treeViewer.getTree();
tree_1.setBounds(316, 205, 244, 446);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","xxxx","xxxx");
Statement stmt2 = con2.createStatement();
String query2="select DISTINCT xxx FROM xxxxx";
ResultSet rs2=stmt2.executeQuery(query2);
while(rs2.next()) {
String executive=rs2.getString("xxxx");
TreeItem item4=new TreeItem(tree_1,SWT.NONE);
item4.setText(executive);
item4.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
}
}
I need to perform the following task:
On selection of a single tree item in a treeViewer, relevant data is supposed to display in the tableViewer.
I have retrieved data from MySQL, using the rs.getString() method, and added the obtained string array into the tree item using the setText() method.
I need to select one of the string values displayed in the treeViewer that has been dynamically generated.
On using a selection event for the tree, all generated elements get selected. How can I obtain a single selection?
Is there any method similar to getSelectedItem() for a treeViewer?
This is the code i used to generate the tree items:
Tree tree_1 = treeViewer.getTree();
tree_1.setBounds(316, 205, 244, 446);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","xxxx","xxxx");
Statement stmt2 = con2.createStatement();
String query2="select DISTINCT xxx FROM xxxxx";
ResultSet rs2=stmt2.executeQuery(query2);
while(rs2.next()) {
String executive=rs2.getString("xxxx");
TreeItem item4=new TreeItem(tree_1,SWT.NONE);
item4.setText(executive);
item4.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.NORMAL));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论