我正在开发 NetBeans 平台应用程序,我希望在应用程序启动时自动打开 TopComponent。尽管遵循了典型步骤,但我的 TopComponent 在启动时并未打开。
以下是我遵循的步骤:
创建了顶部组件:
package com.example.myapp;
import org.openide.windows.TopComponent;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
@TopComponent.Description(
preferredID = "MyTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(
mode = "editor",
openAtStartup = false // I handle the opening manually
)
@ActionID(category = "Window", id = "com.example.myapp.MyTopComponent")
@ActionReference(path = "Menu/Window", position = 0)
@Messages({
"CTL_MyTopComponent=My TopComponent",
"HINT_MyTopComponent=This is a MyTopComponent window"
})
public final class MyTopComponent extends TopComponent {
public MyTopComponent() {
initComponents();
setName(Bundle.CTL_MyTopComponent());
setToolTipText(Bundle.HINT_MyTopComponent());
}
private void initComponents() {
// Initialize your components here
}
public static synchronized MyTopComponent getInstance() {
MyTopComponent instance = (MyTopComponent) WindowManager.getDefault().findTopComponent("MyTopComponent");
if (instance == null) {
instance = new MyTopComponent();
}
return instance;
}
}
创建了一个Installer类来处理启动逻辑:
package com.example.myapp;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;
public class Installer extends ModuleInstall {
@Override
public void restored() {
WindowManager.getDefault().invokeWhenUIReady(() -> {
MyTopComponent tc = MyTopComponent.getInstance();
if (tc != null) {
tc.open();
tc.requestActive();
}
});
}
}
Manifest-Version: 1.0
OpenIDE-Module: com.example.myapp
OpenIDE-Module-Specification-Version: 1.0
OpenIDE-Module-Install: com/example/myapp/Installer.class
尽管执行了这些步骤,TopComponent 仍不会在启动时打开。以下是我为调试该问题所采取的其他详细信息和步骤:
什么可能导致 TopComponent 在启动时无法打开?如何解决此问题?
任何帮助或建议将不胜感激。
使用 ModuleInstall/restored() 可能还为时过早,Netbeans UI 尚未准备好。
相反,请尝试使用 @onShowing 进行相同的操作,以便在 UI 准备就绪时调用您的代码:
@onShowing
static public class OpenStart implements Runnable {
@Override
public void run() {
WindowManager.getDefault().invokeWhenUIReady(() -> {
MyTopComponent tc = MyTopComponent.getInstance();
if (tc != null) {
tc.open();
tc.requestActive();
}
});
}
}