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

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

JTree not multi-selectable if it is editable

问题

以下是翻译好的内容:

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

  1. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  2. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
  3. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

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

  1. _myTree.setEditable(true);

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

完整代码如下:

  1. public class TreeSelectionOption {
  2. public static void main(String[] argv) {
  3. JTree tree = new JTree();
  4. tree.getSelectionModel().setSelectionMode(
  5. TreeSelectionModel.SINGLE_TREE_SELECTION);
  6. tree.getSelectionModel().setSelectionMode(
  7. TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
  8. tree.getSelectionModel().setSelectionMode(
  9. TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  10. DefaultMutableTreeNode book = null;
  11. DefaultMutableTreeNode category = null;
  12. DefaultMutableTreeNode top = (DefaultMutableTreeNode)tree.getModel().getRoot();
  13. category = new DefaultMutableTreeNode("Books for Java Programmers");
  14. top.add(category);
  15. //original Tutorial
  16. book = new DefaultMutableTreeNode(new BookInfo
  17. ("The Java Tutorial: A Short Course on the Basics",
  18. "tutorial.html"));
  19. category.add(book);
  20. //Tutorial Continued
  21. book = new DefaultMutableTreeNode(new BookInfo
  22. ("The Java Tutorial Continued: The Rest of the JDK",
  23. "tutorialcont.html"));
  24. category.add(book);
  25. //JFC Swing Tutorial
  26. book = new DefaultMutableTreeNode(new BookInfo
  27. ("The JFC Swing Tutorial: A Guide to Constructing GUIs",
  28. "swingtutorial.html"));
  29. category.add(book);
  30. //Bloch
  31. book = new DefaultMutableTreeNode(new BookInfo
  32. ("Effective Java Programming Language Guide",
  33. "bloch.html"));
  34. category.add(book);
  35. //Arnold/Gosling
  36. book = new DefaultMutableTreeNode(new BookInfo
  37. ("The Java Programming Language", "arnold.html"));
  38. category.add(book);
  39. tree.addMouseListener(new MouseAdapter() {
  40. @Override
  41. public void mouseClicked(MouseEvent e) {
  42. // TODO Auto-generated method stub
  43. super.mouseClicked(e);
  44. }
  45. });
  46. tree.setCellEditor(createTreeCellEditor() );
  47. tree.setEditable(true);
  48. JFrame frame = new JFrame();
  49. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. frame.add(new JScrollPane(tree));
  51. frame.setSize(380, 320);
  52. frame.setLocationRelativeTo(null);
  53. frame.setVisible(true);
  54. }
  55. protected static TreeCellEditor createTreeCellEditor() {
  56. JTextField tf = new JTextField() {
  57. @Override
  58. public String getName() {
  59. return "Tree.cellEditor";
  60. }
  61. };
  62. DefaultCellEditor editor = new DefaultCellEditor(tf);
  63. // One click to edit.
  64. editor.setClickCountToStart(1);
  65. return editor;
  66. }
  67. private static class BookInfo {
  68. public String bookName;
  69. public String bookURL;
  70. public BookInfo(String book, String filename) {
  71. bookName = book;
  72. bookURL = filename;
  73. }
  74. public String toString() {
  75. return bookName;
  76. }
  77. }
  78. }

当指定了单元格编辑器并且在树上设置了 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.

  1. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  2. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
  3. _myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

It was not working. I tested by removing the line

  1. _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:

  1. public class TreeSelectionOption {
  2. public static void main(String[] argv) {
  3. JTree tree = new JTree();
  4. tree.getSelectionModel().setSelectionMode(
  5. TreeSelectionModel.SINGLE_TREE_SELECTION);
  6. tree.getSelectionModel().setSelectionMode(
  7. TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
  8. tree.getSelectionModel().setSelectionMode(
  9. TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  10. DefaultMutableTreeNode book = null;
  11. DefaultMutableTreeNode category = null;
  12. DefaultMutableTreeNode top = (DefaultMutableTreeNode)tree.getModel().getRoot();
  13. category = new DefaultMutableTreeNode("Books for Java Programmers");
  14. top.add(category);
  15. //original Tutorial
  16. book = new DefaultMutableTreeNode(new BookInfo
  17. ("The Java Tutorial: A Short Course on the Basics",
  18. "tutorial.html"));
  19. category.add(book);
  20. //Tutorial Continued
  21. book = new DefaultMutableTreeNode(new BookInfo
  22. ("The Java Tutorial Continued: The Rest of the JDK",
  23. "tutorialcont.html"));
  24. category.add(book);
  25. //JFC Swing Tutorial
  26. book = new DefaultMutableTreeNode(new BookInfo
  27. ("The JFC Swing Tutorial: A Guide to Constructing GUIs",
  28. "swingtutorial.html"));
  29. category.add(book);
  30. //Bloch
  31. book = new DefaultMutableTreeNode(new BookInfo
  32. ("Effective Java Programming Language Guide",
  33. "bloch.html"));
  34. category.add(book);
  35. //Arnold/Gosling
  36. book = new DefaultMutableTreeNode(new BookInfo
  37. ("The Java Programming Language", "arnold.html"));
  38. category.add(book);
  39. tree.addMouseListener(new MouseAdapter() {
  40. @Override
  41. public void mouseClicked(MouseEvent e) {
  42. // TODO Auto-generated method stub
  43. super.mouseClicked(e);
  44. }
  45. });
  46. tree.setCellEditor(createTreeCellEditor() );
  47. tree.setEditable(true);
  48. JFrame frame = new JFrame();
  49. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. frame.add(new JScrollPane(tree));
  51. frame.setSize(380, 320);
  52. frame.setLocationRelativeTo(null);
  53. frame.setVisible(true);
  54. }
  55. protected static TreeCellEditor createTreeCellEditor() {
  56. JTextField tf = new JTextField() {
  57. @Override
  58. public String getName() {
  59. return "Tree.cellEditor";
  60. }
  61. };
  62. DefaultCellEditor editor = new DefaultCellEditor(tf);
  63. // One click to edit.
  64. editor.setClickCountToStart(1);
  65. return editor;
  66. }
  67. private static class BookInfo {
  68. public String bookName;
  69. public String bookURL;
  70. public BookInfo(String book, String filename) {
  71. bookName = book;
  72. bookURL = filename;
  73. }
  74. public String toString() {
  75. return bookName;
  76. }
  77. }
  78. }

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到树中。

  1. public class JTree示例 {
  2. public static void main(String[] argv) {
  3. JTree = new JTree();
  4. .getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  5. JFrame 框架 = new JFrame("JTree多选");
  6. 框架.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. 框架.add(new JScrollPane());
  8. 框架.setPreferredSize(new Dimension(400, 400));
  9. 框架.setLocation(200, 200);
  10. 框架.pack();
  11. 框架.setVisible(true);
  12. .setEditable(true);
  13. }
  14. }

之后,您可以使用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.

  1. public class JTreeExample {
  2. public static void main(String[] argv) {
  3. JTree tree = new JTree();
  4. tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  5. JFrame frame = new JFrame("JTree multi selection");
  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. frame.add(new JScrollPane(tree));
  8. frame.setPreferredSize(new Dimension(400, 400));
  9. frame.setLocation(200, 200);
  10. frame.pack();
  11. frame.setVisible(true);
  12. tree.setEditable(true);
  13. }
  14. }

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:

确定