# [列表和树控件](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()`并添加它返回到面板的组件。
- 空白目录
- 测试
- ReadMe
- Idea
- Introduction介绍
- Part01-Plugins插件
- 创建你第一个插件
- 用github模版
- 用Gradle
- Gradle入门
- 配置Gradle项目
- 使用Gradle发布插件
- 用DevKit
- 创建开发环境
- 创建一个插件项目
- 运行和debug一个插件
- 部署一个插件
- 发布一个插件
- IDE开发实例
- 将插件发布到自定义插件存储库
- 插件结构
- 插件内容
- 类加载器
- Actions
- 扩展
- 服务
- 监听器
- 扩展点
- Components
- 插件配置文件
- 插件Logo
- 依赖
- 动态插件
- IntelliJ平台工件库
- 插件开发者的 Kotlin
- 插件签名
- 内部Actions菜单
- 启用内部模式
- 内部操作 - UI 子菜单
- 内部操作 - UI 检查器
- 内部操作 - LaF 默认值
- 性能优化
- 插件开发常见问题
- Part02-基础平台
- 基础
- 处理器和一次性
- 一般线程规则
- 消息基础设施
- IDE基础设施
- 用户界面组件
- 工具窗口
- 对话框
- Popups
- 通知
- 文件和类选择器
- 编辑器组件
- 列表和树控件
- 其他Swing组件
- 使用图标和图像
- 配色方案管理
- Kotlin 用户界面 DSL
- JCEF - Java Chromium 嵌入式框架
- 自定义 UI 主题
- 创建自定义 UI 主题
- 自定义 UI 主题 - 图标和 UI 控件
- UI 主题 - 编辑器方案和背景图像
- 公开主题元数据
- Actions
- Actions教程
- 创建Actions
- Actions分组
- 持久化模型
- 组件的持久状态
- 持久化敏感数据
- 设置
- 设置指南
- 自定义设置组
- 设置教程
- 文件
- 虚拟文件系统
- 虚拟文件
- 文档
- 编辑
- 编辑器基础
- xxx
- 代码实现
- Temp-理解
- xxx