JTree 如果是可编辑的,则无法多选。

huangapple go评论102阅读模式
英文:

JTree not multi-selectable if it is editable

问题

以下是翻译好的内容:

我有一个具有单选功能的 JTree。我想使树具备多选功能。我添加了以下内容来实现这一点。

_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

但这并没有起作用。我尝试删除了这行代码:

_myTree.setEditable(true);

然后多选功能就正常工作了。只有当树的可编辑状态为 false 时,我才能够多选树节点。有没有办法修复这个问题?

完整代码如下:

public class TreeSelectionOption {
  public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(
                             TreeSelectionModel.SINGLE_TREE_SELECTION);

    tree.getSelectionModel().setSelectionMode(
                            TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
    tree.getSelectionModel().setSelectionMode(
                         TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    DefaultMutableTreeNode book = null;
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode top = (DefaultMutableTreeNode)tree.getModel().getRoot();
    category = new DefaultMutableTreeNode("Books for Java Programmers");
    top.add(category);

    //original Tutorial
    book = new DefaultMutableTreeNode(new BookInfo
        ("The Java Tutorial: A Short Course on the Basics",
        "tutorial.html"));
    category.add(book);

    //Tutorial Continued
    book = new DefaultMutableTreeNode(new BookInfo
        ("The Java Tutorial Continued: The Rest of the JDK",
        "tutorialcont.html"));
    category.add(book);

    //JFC Swing Tutorial
    book = new DefaultMutableTreeNode(new BookInfo
        ("The JFC Swing Tutorial: A Guide to Constructing GUIs",
        "swingtutorial.html"));
    category.add(book);

    //Bloch
    book = new DefaultMutableTreeNode(new BookInfo
        ("Effective Java Programming Language Guide",
        "bloch.html"));
    category.add(book);

    //Arnold/Gosling
    book = new DefaultMutableTreeNode(new BookInfo
        ("The Java Programming Language", "arnold.html"));
    category.add(book);
    tree.addMouseListener(new MouseAdapter() {
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		super.mouseClicked(e);
    	}
	});
    tree.setCellEditor(createTreeCellEditor() );
    tree.setEditable(true);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  protected static TreeCellEditor createTreeCellEditor() {
    JTextField tf = new JTextField() {
        @Override
        public String getName() {
            return "Tree.cellEditor";
        }
    };
    DefaultCellEditor editor = new DefaultCellEditor(tf);

    // One click to edit.
    editor.setClickCountToStart(1);
    return editor;
  }

  private static class BookInfo {
    public String bookName;
    public String bookURL;

    public BookInfo(String book, String filename) {
        bookName = book;
        bookURL = filename;
    }

    public String toString() {
        return bookName;
    }
  }
}

当指定了单元格编辑器并且在树上设置了 setEditable(true) 后,多选功能将停止工作。

英文:

I have a JTree that had single selection. I want to make the tree multi-selectable. I added the following to do that.

_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
_myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

It was not working. I tested by removing the line

_myTree.setEditable(true);

and it works.
I can multiselect the nodes in a tree only if it is not editable. Is there a way to fix this?

See full code here:

public class TreeSelectionOption {
public static void main(String[] argv) {
JTree tree = new JTree();
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
DefaultMutableTreeNode book = null;
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode top = (DefaultMutableTreeNode)tree.getModel().getRoot();
category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);
//original Tutorial
book = new DefaultMutableTreeNode(new BookInfo
("The Java Tutorial: A Short Course on the Basics",
"tutorial.html"));
category.add(book);
//Tutorial Continued
book = new DefaultMutableTreeNode(new BookInfo
("The Java Tutorial Continued: The Rest of the JDK",
"tutorialcont.html"));
category.add(book);
//JFC Swing Tutorial
book = new DefaultMutableTreeNode(new BookInfo
("The JFC Swing Tutorial: A Guide to Constructing GUIs",
"swingtutorial.html"));
category.add(book);
//Bloch
book = new DefaultMutableTreeNode(new BookInfo
("Effective Java Programming Language Guide",
"bloch.html"));
category.add(book);
//Arnold/Gosling
book = new DefaultMutableTreeNode(new BookInfo
("The Java Programming Language", "arnold.html"));
category.add(book);
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
}
});
tree.setCellEditor(createTreeCellEditor() );
tree.setEditable(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(tree));
frame.setSize(380, 320);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected static TreeCellEditor createTreeCellEditor() {
JTextField tf = new JTextField() {
@Override
public String getName() {
return "Tree.cellEditor";
}
};
DefaultCellEditor editor = new DefaultCellEditor(tf);
// One click to edit.
editor.setClickCountToStart(1);
return editor;
}
private static class BookInfo {
public String bookName;
public String bookURL;
public BookInfo(String book, String filename) {
bookName = book;
bookURL = filename;
}
public String toString() {
return bookName;
}
}
}

When the cell editor is specified and the setEditable(true) is set on the tree then multiselect stops working.

答案1

得分: 1

将选择模型添加为TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION到树中。

public class JTree示例 {

    public static void main(String[] argv) {
        JTree 树 = new JTree();
.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        JFrame 框架 = new JFrame("JTree多选");
        框架.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        框架.add(new JScrollPane());
        框架.setPreferredSize(new Dimension(400, 400));
        框架.setLocation(200, 200);
        框架.pack();
        框架.setVisible(true);
.setEditable(true);
    }
}

之后,您可以使用TreeSelectionListener来获取所选节点。

https://docs.oracle.com/javase/7/docs/api/javax/swing/event/TreeSelectionListener.html

英文:

Add selection model as TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION to the tree.

public class JTreeExample {
public static void main(String[] argv) {
JTree tree = new JTree();
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
JFrame frame = new JFrame("JTree multi selection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(tree));
frame.setPreferredSize(new Dimension(400, 400));
frame.setLocation(200, 200);
frame.pack();
frame.setVisible(true);
tree.setEditable(true);
}
}

After you can use TreeSelectionListener to get the selected node.

https://docs.oracle.com/javase/7/docs/api/javax/swing/event/TreeSelectionListener.html

huangapple
  • 本文由 发表于 2020年4月6日 18:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定