Over a million developers have joined DZone.

Bye JList!

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

If you're creating applications on top of the NetBeans Platform, you need to bid a fond farewell to the JList. The NetBeans Platform provides an equivalent that is much more powerful than the JList was ever meant to be. Here, in a TopComponent, is a JList:

 

And, after deleting the JList above, replacing it with org.openide.explorer.view.ListView, here's the same application again:

 

In terms of code, the difference is that the ListView automatically displays a Node, and remains synchronized with other explorer views, such as the Properties window. In the case of the JList, this is all the code I needed to provide... which is a class I now no longer need:

import java.beans.PropertyVetoException;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;

public class CustomerScrollPane extends JScrollPane {

    private ExplorerManager em;
    private JList list;

    @Override
    public void addNotify() {
        super.addNotify();
        //The JScrollPane is created within a TopComponent
        //that implements ExplorerManager.Provider, which
        //gives you access to the root node of your hierarchy,
        //which you can find as follows:
        em = ExplorerManager.find(this);
        if (em != null) {
            list = new JList();
            //Get the root of the ExplorerManager:
            Node root = em.getRootContext();
            //Get the children of the root node:
            final Node[] nodes = root.getChildren().getNodes();
            //Create a string array, using the number of child nodes:
            final String[] nodeNames = new String[nodes.length];
            //Add the names of the child nodes to the array:
            for (int i = 0; i < nodes.length; i++) {
                String nodeName = nodes[i].getDisplayName();
                nodeNames[i] = nodeName;
            }
            //Create the ListModel, using the array created above:
            list.setModel(new javax.swing.AbstractListModel() {
                @Override
                public int getSize() {
                    return nodeNames.length;
                }

                @Override
                public Object getElementAt(int i) {
                    return nodeNames[i];
                }
            });
            //Whenever a new item is selected in the list,
            //set the related node as the current node,
            //so that the Properties window is updated:
            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    for (int i = 0; i < nodes.length; i++) {
                        String nodeName = nodes[i].getDisplayName();
                        if (list.getSelectedValue().equals(nodeName)){
                            try {
                                em.setSelectedNodes(new Node[]{nodes[i]});
                            } catch (PropertyVetoException ex) {
                                Exceptions.printStackTrace(ex);
                            }
                        }
                    }
                }
            });
            //Add the JList to the JScrollPane:
            setViewportView(list);
        }

    }

}
 

 

All of the above code is superfluous. Simply use a ListView instead and the ExplorerManager will do all the work for you. If you don't understand what that means, you clearly have not followed the NetBeans Platform CRUD Application Tutorial yet. Do so now!

Note: The two attachments below (the first using JList, the second ListView) contain the code that provided the two screenshots above.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}