🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# [列表和树控件](https://plugins.jetbrains.com/docs/intellij/lists-and-trees.html) <br> ## JBList 和树[](https://plugins.jetbrains.com/docs/intellij/lists-and-trees.html#jblist-and-tree) 每当您通常使用标准[Swing`JList`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html)组件时,建议使用[`JBList`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/components/JBList.java)该类作为替代品。[`JBList`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/components/JBList.java)支持以下附加功能[`JList`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html): * 如果项目不适合列表框宽度,则绘制带有项目完整文本的工具提示。 * 当列表框不包含任何项目时,在列表框中间绘制灰色文本消息。可以通过调用自定义文本`getEmptyText().setText()`。 * 在列表框的右上角画一个忙碌图标,表示正在执行后台操作。这可以通过调用启用`setPaintBusy()`。 <br> 同样,[`Tree`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/treeStructure/Tree.java)该类提供了标准[`JTree`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTree.html)类的替代品。除了 的功能外[`JBList`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/components/JBList.java),它还支持广泛选择绘画(Mac 风格)和拖放时自动滚动。 <br> ***** ## ColoredListCellRenderer 和 ColoredTreeCellRenderer[](https://plugins.jetbrains.com/docs/intellij/lists-and-trees.html#coloredlistcellrenderer-and-coloredtreecellrenderer) 当您需要自定义列表框或树中项目的呈现方式时,建议使用[`ColoredListCellRenderer`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/ColoredListCellRenderer.java)或[`ColoredTreeCellRenderer`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/ColoredTreeCellRenderer.java)类作为单元格渲染器。这些类允许您通过调用由具有不同属性的多个文本片段组成演示文稿,`append()`并通过调用为项目设置可选图标`setIcon`。渲染器会自动为所选项目和许多其他特定于平台的渲染细节设置正确的文本颜色。 <br> ***** ## ListSpeedSearch 和 TreeSpeedSearch[](https://plugins.jetbrains.com/docs/intellij/lists-and-trees.html#listspeedsearch-and-treespeedsearch) 为了便于在列表框或树中基于键盘选择项目,您可以使用[`ListSpeedSearch`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-impl/src/com/intellij/ui/ListSpeedSearch.java)和在其上安装速度搜索处理程序[`TreeSpeedSearch`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java)。这可以通过调用`new ListSpeedSearch(list)`或来完成`new TreeSpeedSearch(tree)`。如果您需要自定义用于定位元素的文本,您可以覆盖该`getElementText()`方法。或者,您可以传递一个函数来将项目转换为字符串。函数需要将其交给`elementTextDelegate`在[`ListSpeedSearch`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-impl/src/com/intellij/ui/ListSpeedSearch.java)构造函数或`toString`到[`TreeSpeedSearch`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java)构造函数。 <br> ***** ## 工具栏装饰器[](https://plugins.jetbrains.com/docs/intellij/lists-and-trees.html#toolbardecorator) 插件开发中一个非常常见的任务是显示一个列表或一棵树,允许用户添加、删除、编辑或重新排序项目。[`ToolbarDecorator`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java)该类极大地促进了该任务的实现。此类提供了一个带有项目操作的工具栏,并在基础列表模型支持的情况下自动启用列表框中项目的拖放重新排序。工具栏在列表上方或下方的位置取决于运行 IDE 的平台。 <br> 要使用工具栏装饰器: * 如果您需要支持列表框中项目的删除和重新排序,请确保您的列表模型实现了该[`EditableModel`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/util/ui/src/com/intellij/util/ui/EditableModel.java)接口。[`CollectionListModel`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/util/ui/src/com/intellij/ui/CollectionListModel.java)是一个方便的模型类,它实现了这个接口。 * 调用[`ToolbarDecorator.createDecorator()`](https://upsource.jetbrains.com/idea-ce/file/idea-ce-f3337ede11680b745f513943772150ee781a51c0/platform/platform-api/src/com/intellij/ui/ToolbarDecorator.java)以创建装饰器实例。 * 如果您需要支持添加和/或删除项目,请致电`setAddAction()`和/或`setRemoveAction()`。 * 如果您需要除标准按钮之外的其他按钮,请致电`addExtraAction()`或`setActionGroup()`。 * 调用`createPanel()`并添加它返回到面板的组件。