Bye JList!
Join the DZone community and get the full member experience.
Join For FreeIf 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.
Opinions expressed by DZone contributors are their own.
Trending
-
Designing a New Framework for Ephemeral Resources
-
How To Manage Vulnerabilities in Modern Cloud-Native Applications
-
Microservices: Quarkus vs Spring Boot
-
Zero Trust Network for Microservices With Istio
Comments