BookNBPlatformCookbookCH0211
Contents |
NetBeans Platform Cookbook Chapter 02 11
Using Status Bar
Some final message (e. g. task completed) can be displayed in the Status Bar of the application. You find it at the bottom of the main window. Each message is shown a while. Then is disappeared or replaced by another one.
Using Status Bar needs dependency on UI Utilities module.
StatusDisplayer is an extension point so you can define your own as a service (see Lookup chapter).
How to
To display some message
StatusDisplayer.getDefault().setStatusText(String text);
Installing your component into Status Bar
The Status Bar has an extension point - if the provider for StatusLineElementProvider service is found it asks it for component and this component is added into status bar. We try show status of the database connection here by semaphore icon with corresponding tooltip.
Create insance of StatusLineElementProvider and register it by an annotation or in the META_INF/services folder.
@ServiceProvider(service=StatusLineElementProvider.class) public class ConnectionStateLine implements StatusLineElementProvider { private Component component = ConnectionState.getInstance().getComponent(); @Override public Component getStatusLineElement() { return component; } }
Create the ConnectionState class and provide JLabel as a component. Switch semaphore icon according to connection state. (In real it would be made otherwise.)
public class ConnectionState { private int connectionState = STATE_NOTCONN; private JLabel label; public static final int STATE_NOTCONN = 0; public static final int STATE_STARTING = 1; public static final int STATE_STOPPING = 2; public static final int STATE_CONNECTED = 3; private static final String [] STATE_NAMES_KEYS = { "STATE_NOTCONN", "STATE_STARTING" , "STATE_STOPPING", "STATE_CONNECTED" }; private static String [] STATE_NAMES ; private static final ImageIcon imgConnected; private static final ImageIcon imgStartStop; private static final ImageIcon imgNotconn; // property change support static { imgConnected = new ImageIcon( ImageUtilities.loadImage( "nbpcook/status/connected.png") ); imgStartStop = new ImageIcon( ImageUtilities.loadImage( "nbpcook/status/startstop.png") ); imgNotconn = new ImageIcon( ImageUtilities.loadImage( "nbpcook/status/notconn.png") ); STATE_NAMES = new String [STATE_NAMES_KEYS.length]; for (int i = 0; i < STATE_NAMES_KEYS.length; i++) { STATE_NAMES [i] = NbBundle.getMessage( ConnectionState.class , STATE_NAMES_KEYS[i] ); } // for i STATE_NAMES_KEYS } private static final ConnectionState instance = new ConnectionState(); public static ConnectionState getInstance() { return instance; } public String getStateName(int state) { return STATE_NAMES [state]; } protected ConnectionState() { this.label = new JLabel(imgNotconn); this.connectionState = STATE_NOTCONN; this.label.setToolTipText( NbBundle.getMessage(ConnectionState.class , "DB_CONNECTION_STATUS" , getStateName(this.connectionState)) ); } JComponent getComponent() { return this.label; } public int getConnectionState() { return connectionState; } public void setConnectionState(int connectionState) { // check parameter ... this.connectionState = connectionState; // propertyChangeSupport ... Icon icon = imgNotconn; switch (this.connectionState) { case STATE_CONNECTED : icon = imgConnected; break; case STATE_STARTING : case STATE_STOPPING : icon = imgStartStop; break; default : icon = imgNotconn; break; } // switch this.connectionState this.label.setIcon(icon); this.label.setToolTipText( NbBundle.getMessage(ConnectionState.class , "DB_CONNECTION_STATUS" , getStateName(this.connectionState)) ); } }
Now you can set state of the DB connection:
ConnectionState.getInstance().setConnectionState(state);
Figure 2.19 Extended Status Bar
Notes/Tips
Text and sources were created under NB 6.8, 6.9, 7.0, 7.1, 7.2.
Navigation
- Home to NetBeans Platform Cookbook
- up to User Interface
- previous to Using Progress Bar
- next to Lookup