DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

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

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
  1. DZone
  2. Coding
  3. Tools
  4. Bye JList!

Bye JList!

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Jul. 06, 10 · News
Like (0)
Save
Tweet
Share
9.39K Views

Join the DZone community and get the full member experience.

Join For Free

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.

application NetBeans Property (programming)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: