我正在使用 NetBeans 平台开发桌面应用程序。该应用程序会将地图放置在顶部组件中,用户可以在其上绘制布局和其他元素。元素存储在 hashmap 中。它们通过鼠标手势添加到哈希图中。
此时,我需要在 UI 上按下按钮时检索一个 hashmap 对象。按钮在 中moduleA
,hashmap、映射和对象在 中定义moduleB
。我正在尝试通过使用ServiceProvider
模式来做到这一点。所以我的代码如下:
package org.dhviz.design.gui.buttonToolbar;
//...annotations here...
public final class AssociateConsToPipes implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Collection<? extends DhvizJMapViewerInterface> allHandlers1 = Lookup.
getDefault().lookupAll(DhvizJMapViewerInterface.class);
for (DhvizJMapViewerInterface chi1 : allHandlers1) {
System.out.println(chi1.getMapConsumerHashMap());
}
}
}
实现接口的类就是这么定义的,放在Module B中:
@ServiceProvider(service = DhvizJMapViewerInterface.class)
public class DhvizJMapViewer
extends JMapViewer implements DhvizJMapViewerInterface {
protected LinkedHashMap<Integer, MapMarkerElementImpl> mapSourceHashMap = null;
protected LinkedHashMap<Integer, MapMarkerElementImpl> mapConsumerHashMap = null;
protected LinkedHashMap<Integer, MapPipeImpl> mapPipeHashMap = null;
public DhvizJMapViewer() {
this(new MemoryTileCache(), 8);
//map controllers for interaction on the map
mapController = new DefaultDhvizMapController(this);
this.setScrollWrapEnabled(false);
}
public DhvizJMapViewer(TileCache tileCache, int downloadThreadCount) {
//some init code here
}
我希望在按下按钮时得到哈希图,但我总是得到一个空对象,尽管我可以看到哈希图不是空的。
谁能解释一下为什么?非常感谢