3

I am using the NetBeans Platform and Java to develop an Application. At the bottom of the application there is a StatusDisplayer, that is always included by default. To this StatusDisplayer I have added a panel (StatusBarJPanel). This StatusBarJPanel has two image icons, indicating network activity.

I don't know how to hide (setVisible(false)) these icons on this panel created at runtime when there is no activity...how to update the two icons on the StatusBarJPanel.

The following is the code that adds my StatusBarJPanel to the StatusDisplayer:

import java.awt.Component;
import gui.StatusBarJPanel;
import org.openide.awt.StatusLineElementProvider;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = StatusLineElementProvider.class)

public class StatusBar implements StatusLineElementProvider {

    private StatusBarJPanel statusBarBottomJPanel = new StatusBarJPanel();

    @Override
    public Component getStatusLineElement() {
        return statusBarBottomJPanel;
    }
}
jadrijan
  • 1,438
  • 4
  • 31
  • 48

2 Answers2

3

If I'm understanding your requirements correctly, you should be able to do the following:

Collection<? extends StatusLineElementProvider> all = 
    Lookup.getDefault().lookupAll(StatusLineElementProvider.class);
for (StatusLineElementProvider a : all) {
    if (a instanceof StatusBar) {
        StatusBarJPanel ele = (StatusBarJPanel ) ((StatusBar) a).getStatusLineElement();
        ele.doUpdate(); // or whatever method you need to call
    }
}

Using this technique will give you the same StatusLineElementProviders that were registered with the status line at startup.

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
2

You don't show your StatusBarJPanel. Presumably it contains two JComponents, e.g. JLabel, each of which has an ImageIcon. It should be straightforward to use setIcon(icon) to change the Icon or setIcon(null) to make the icon disappear. The examples shown here and here can be used to test the effect.

As an aside, you can specify the position in the @ServiceProvider annotation, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for this. I probably did not explain my problem right. Yes, I have 2 JLabels on the StatusBarJPanel. I am wondering how to "signal" to show/hide these icons once the application is already running? I tried using Interfaces but could not figure out how. On startup the application shows the panel, but how can I update this panel later? – jadrijan Jul 13 '12 at 13:03
  • 1
    Also consider _loose coupling_ as an alternative, mentioned [here](http://stackoverflow.com/a/11399506/230513). – trashgod Jul 13 '12 at 17:37