如何在动态生成的树项目上执行单个选择?

发布于 2025-01-17 18:27:54 字数 916 浏览 3 评论 0原文

我需要执行以下任务: 在树查看器中选择单个树项目时,相关数据应该显示在表查看器中。 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文