java多选_java – jtree程序化多选

是否能够以编程方式在JTree中选择多个树节点?

我已经通过tree.getSelectionModel()设置了多选模式.setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

而我所需要的一切 – 让我的应用程序能够以编程方式选择一些节点.但我没有找到如何做到这一点的方式.任何人都可以提供建议如何解决这个问题?

谢谢

import java.awt.Dimension;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.tree.*;

public class TreeWithMultiDiscontiguousSelections {

public static void main(String[] argv) {

JTree tree = new JTree();

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

int treeSelectedRows[] = {3, 1};

tree.setSelectionRows(treeSelectedRows);

TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

@Override

public void valueChanged(TreeSelectionEvent treeSelectionEvent) {

JTree treeSource = (JTree) treeSelectionEvent.getSource();

System.out.println("Min: " + treeSource.getMinSelectionRow());

System.out.println("Max: " + treeSource.getMaxSelectionRow());

System.out.println("Lead: " + treeSource.getLeadSelectionRow());

System.out.println("Row: " + treeSource.getSelectionRows()[0]);

}

};

tree.addTreeSelectionListener(treeSelectionListener);

JFrame frame = new JFrame("JTree With Multi-Discontiguous selection");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new JScrollPane(tree));

frame.setPreferredSize(new Dimension(380, 320));

frame.setLocation(150, 150);

frame.pack();

frame.setVisible(true);

}

private TreeWithMultiDiscontiguousSelections() {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值