2012-07-12 80 views
3

我正在使用NetBeans平台和Java开发应用程序。在应用程序的底部有一个StatusDisplayer,默认情况下总是包含它。对于这个StatusDisplayer我添加了一个面板(StatusBarJPanel)。这个StatusBarJPanel有两个图标图标,表示网络活动。NetBeans平台和StatusLineElementProvider

我不知道如何隐藏(setVisible(false))这个面板上的这些图标在运行时创建的时候没有任何活动......如何更新StatusBarJPanel上的两个图标。

下面是我添加到StatusBarJPanel的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; 
    } 
} 

回答

3

如果我正确理解你的要求,你应该能够做到以下几点:

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 
    } 
} 

使用这种技术将会给你用在启动状态行注册了相同的StatusLineElementProvider秒。

+0

非常感谢你,这就是我一直在寻找的,它完美的作品! – jadrijan 2012-07-13 15:07:55

2

你不显示您的StatusBarJPanel。推测它包含两个JComponent,例如, JLabel,其中每个都有ImageIcon。使用setIcon(icon)更改IconsetIcon(null)以使图标消失应该很简单。示例herehere可用于测试效果。

另外,您可以在@ServiceProvider注释中指定position,如here所示。

+0

感谢您的支持。我可能没有正确解释我的问题。是的,我在StatusBarJPanel上有两个JLabel。我想知道如何“信号”显示/隐藏这些图标,一旦应用程序已经运行?我尝试使用接口,但无法弄清楚如何。启动时,应用程序会显示面板,但以后如何更新此面板? – jadrijan 2012-07-13 13:03:44

+1

也可以考虑_loose coupling_作为替代方法,在[这里]提到(http://stackoverflow.com/a/11399506/230513)。 – trashgod 2012-07-13 17:37:42