Question

I'm running in a quite strange problem while programming with the Netbean platform 7.3, based on Geertjan's blog example on how to add widgets through actions (https://blogs.oracle.com/geertjan/entry/add_widget_via_action_in) :

I created a TopComponent, and I want it to show a custom ScrollPane rendering images in a custom pane. My custom Scrollpane is working nice in a basic Java Swing application.

I tried to display the Scrollpane in the TopComponent's constructor, just to test :

  public TopComponent()
    {
        super();
        setBackground( Color.BLUE );
        initComponents();
        setName( Bundle.CTL_Scanmage2TopComponent() );
        setToolTipText( Bundle.HINT_Scanmage2TopComponent() );
        setImage( ImageHandlerFactory.getImageHandler( new File( ... ) ));
    }

There is the setImage method the ImageHandler is a class of mine describing an image :

public void setImage( ImageHandler handler ) throws Exception
{
    pane = new Scrollpane(); // pane is a class field of type : Scrollpane

    pane.setImage( handler ); // thats OK, the tiled image is loaded, etc.

    setLayout( new BorderLayout() );

    add( pane , BorderLayout.CENTER , 0 );

    pane.revalidate(); // does not work
    pane.repaint(); // does not work
}

All OK, the netbeans application launches, the image is well displayed in the application only when setImage is called from constructor or from componentOpened.

Now, I would load the image through a menu, I implemented an interface, a service provider, all OK.

From the menu action, I use the lookup to find all objects having the method to load the image through the ImageHandler generated from this action.

Then, calling setImage(...) through the Lookup does nothing, my TopComponent absolutely refuses to display anything.

I tried to use a FlowLayout, thus displaying a small (about 3x3 pix) thing in the top of the TopComponent.

The question is : how can I force the TopComponent to paint the Scrollpane (extends JScrollpane) I put into it ?

I tried to revalidate / validate / invalidate / repaint / ... the TopComponent, the Scrollpane, but no way...

Any idea ?

Thanks !

-- EDIT 1 / reedited : unuseful parts deleted --

Geertjan's app still works fine, but calling the setImage method from the button's action does nothing.

Here is the code of this button :

public void actionPerformed( ActionEvent e )
{
    Lookup.Result<MyInterface > controllers = Lookup.getDefault().lookupResult( MyInterface .class );
    Collection<? extends MyInterface > controllersColl = controllers.allInstances();
    for( MyInterface c : controllersColl )
    {
        c.setImage( imgH ); // debug shows it passes heres and expected internal job is done
    }
}

All seems to go OK, objects are created, etc. but nothing visible happens in the GUI.

-- EDIT 2 / reedited : unuseful parts deleted --

-- EDIT 3 / after Tan Hui Onn answer --

Indeed, that was it, thanks Tan Hui Onn : "you will just get another instance of TopComponent".

Using TopComponent.getRegistry().getActivated() does the job.

Was it helpful?

Solution

I don't think you can get the opened TopComponent through @ServiceProvider. I think you will just get another instance of TopComponent.

For testing, I add this line ((ImageDisplayerCapability) MyModuleTopComponent.getRegistry().getActivated()).setImage(imgH); in OpenFileActionWhichDontWork.actionPerformed. It shows something (but I am not sure it is correct or not).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top