I'm working on a NetBeans Platform application and I want to open a TopComponent automatically when the application starts. Despite following the typical steps, my TopComponent does not open at startup.
Here are the steps I have followed:
Created the 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;
}
}
Created an Installer class to handle the startup logic:
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
Despite following these steps, the TopComponent does not open at startup. Here are additional details and steps I've taken to debug the issue:
What could be causing the TopComponent to not open at startup, and how can I resolve this issue?
Any help or suggestions would be greatly appreciated.
Using ModuleInstall/restored() is probably too early, Netbeans UI is not ready yet.
Instead, try the same with @onShowing, so that your code is called when UI is ready:
@onShowing
public class OpenStart implements Runnable {
@Override
public void run() {
WindowManager.getDefault().invokeWhenUIReady(() -> {
MyTopComponent tc = MyTopComponent.getInstance();
if (tc != null) {
tc.open();
tc.requestActive();
}
});
}
}