英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论