我正在编写一个java swing组件(一个扩展面板),我想允许(在memory / jmv中)订阅expand事件或者甚至是崩溃事件。目前是否有推荐的做法?
如果折叠该部分,则只显示标题图标和标题标题。当用户点击图片或标题时,将显示部分内容。展开后,点击标题图标或标题将折叠并隐藏部分内容。
处于崩溃状态:
+---+---------------+
|img| section title |
+---+---------------+
扩张后:
+---+---------------+
|img| section title |
+---+---------------+
| |
| section content |
| |
+-------------------+
当发生这些扩展或崩溃事件时,我想使用接口来通知观察者/听众/订阅者。在内存中应该是充足的,不需要消息代理或类似的东西。
我知道Runnable,ActionListener和Observable。这些是推荐的方法中的任何一种,还是我应该创建自己的方法。在我的应用程序中,监听的人只需要知道分配给面板的“String sectionId”,这样他就可以解析面板的内容,我们将动态填充这些内容。
希望调用者能够使用以下内容注册更新:
CollapsibleSection cSection = new CollapsibleSection();
cSection.addListener(MyClass.this);
到目前为止,我有类似的东西,但我想确保我并不总是重新发明轮子,所以想与专家联系!
public interface CollapsibleSectionListener {
/**
* @param collapsibleSection
* the section that was expanded.
* @param sectionId
* ID of this section that was expanded.
*/
void onSectionExpand(CollapsibleSection collapsibleSection, String sectionId);
/**
* @param collapsibleSection
* the section that was collapsed.
* @param sectionId
* ID of this section that was collapsed.
*/
void onSectionCollapse(CollapsibleSection collapsibleSection, String sectionId);
}