Download NetBeans!

20070413 Friday April 13, 2007

Library List in the IDE's Toolbar

On the face of it, this isn't the most useful functionality. A JComboBox, with a filter, in the IDE's toolbar, for displaying all the libraries in the IDE's Library Manager:

However, knowing how to get the libraries from the Library Manager is pretty useful in some cases. For example, in the Frameworks panel in the New Project wizard for web applications, you might want to show all the libraries (or a filtered list, meeting certain criteria), so that the user can choose one of them to be put on the classpath.

So, in the above scenario, you need to declare a dependency on "External Libraries". This is the code, called from the JPanel's constructor, as well as in the (optional) filter's keyPressed event:

private void initLibraries(){

    Library libraries[] = LibraryManager.getDefault().getLibraries();
    Vector  items = new Vector();
    ArrayList  allLibraries = new ArrayList();

    String filter = jTextField1.getText();

    for (int i = 0; i < libraries.length; i++) {
        if (libraries[i].getName().startsWith(filter)){    //NOI18N
            String displayName = libraries[i].getDisplayName();
            items.add(displayName);
            allLibraries.add(libraries[i]);
        }
    }

    jComboBox1.setModel(new DefaultComboBoxModel(items));
    if (items.size() == 0){
        jComboBox1.setEnabled(false);
    } else {
        jComboBox1.setEnabled(true);
    }
    
    repaint();
}

And how do you create the toolbar in the first place? See the Google Toolbar Module Tutorial for details.

Apr 13 2007, 04:02:04 AM PDT Permalink