Wednesday Sep 30, 2009

NetBeans tip: Notifications using the Dialogs API

The standard code used to display a dialog in NetBeans is:

DialogDescriptor dd = new DialogDescriptor(new UIPanel(), "UI Panel" ) ;
Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
dlg.setVisible(true);

This uses the DialogDescriptor and DialogDisplayer classes from the Dialogs API. You end up a dialog which looks like the image below.

Standard UI Panel

However, what if you want to display some notification messages to the user? NotificationLineSupport to the rescue. Just create a notification line (code highlighted below) and add your message.

DialogDescriptor dd = new DialogDescriptor(new UIPanel(), "UI Panel" ) ;
NotificationLineSupport supp = dd.createNotificationLineSupport();
Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
supp.setInformationMessage("Sample Information Message" ) ;
dlg.setVisible(true);

The result is a dialog like the one shown below:

UI Panel with notifications

You can show informational messages, error messages and warning messages. However, displaying an error or a warning message does not disable the OK button. For that you will have to call the setValid() method.

Saturday Jul 25, 2009

How to display the NetBeans font chooser.

Since I have been going after some low hanging fruits in fixing some Python support related issues in NetBeans, strictly over weekends (without the knowledge of my wife who prefers my MacBook Pro being 2000 miles away from me on weekends), I ran into an interesting issue where I had to display the NetBeans font chooser instead of a custom font chooser being used in the code. Unable to find the proper API (perhaps I am dumb), I waded into the NetBeans sources, and found the way out. Blogging about this here to save some poor soul some time when the need arises.

PropertyEditor pe = PropertyEditorManager.findEditor (Font.class);
DialogDescriptor dd = new DialogDescriptor (
    pe.getCustomEditor(),
    "Some Title"  // NOI18N
);

DialogDisplayer.getDefault ().createDialog (dd).setVisible (true);
if (dd.getValue () == DialogDescriptor.OK_OPTION) {
    font = (Font) pe.getValue ();
}

It just boils down to getting a property editor for Font and then wrapping the panel inside a dialog box using the NetBeans DialogDisplayer API.


About

Rohan Ranade's musings on anything and everything.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today