ComboBox Data Binding

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sat, 2 Jun 2012 08:53:20 +0000 Indexed on 2012/06/02 10:47 UTC
Read the original article Hit count: 177

Filed under:

Let's create a databound combobox, levering MVC in a desktop application.

The result will be a combobox, provided by the NetBeans ChoiceView, that displays data retrieved from a database:


What follows is not much different from the NetBeans Platform CRUD Application Tutorial and you're advised to consult that document if anything that follows isn't clear enough.

One kind of interesting thing about the instructions that follow is that it shows that you're able to create an application where each element of the MVC architecture can be located within a separate module:



Start by creating a new NetBeans Platform application named "MyApplication".

Model

  1. We're going to start by generating JPA entity classes from a database connection. In the New Project wizard, choose "Java Class Library". Click Next. Name the Java Class Library "MyEntities". Click Finish. Right-click the MyEntities project, choose New, and then select "Entity Classes from Database". Work through the wizard, selecting the tables of interest from your database, and naming the package "entities". Click Finish. Now a JPA entity is created for each of the selected tables. In the Project Properties dialog of the project, choose "Copy Dependent Libraries" in the Packaging panel. Build the project. In your project's "dist" folder (visible in the Files window), you'll now see a JAR, together with a "lib" folder that contains the JARs you'll need.

  2. In your NetBeans Platform application, create a module named "MyModel", with code name base "org.my.model". Right-click the project, choose Properties, and in the "Libraries" panel, click Add Dependency button in the Wrapped JARs subtab to add all the JARs from the previous step to the module. Also include "derby-client.jar" or the equivalent driver for your database connection to the module.

Controler

  1. In your NetBeans Platform application, create a module named "MyControler", with code name base "org.my.controler". Right-click the module's Libraries node, in the Projects window, and add a dependency on "Explorer & Property Sheet API".

  2. In the MyControler module, create a class with this content:
    package org.my.controler;
    
    import org.openide.explorer.ExplorerManager;
    
    public class MyUtils {
        
        static  ExplorerManager controler;
    
        public static ExplorerManager getControler() {
            if (controler == null) {
                controler = new ExplorerManager();
            }
            return controler;
        }
        
    }

View

  1. In your NetBeans Platform application, create a module named "MyView", with code name base "org.my.view".  Create a new Window Component, in "explorer" view, for example, let it open on startup, with class name prefix "MyView". Add dependencies on the Nodes API and on the Explorer & Property Sheet API. Also add dependencies on the "MyModel" module and the "MyControler" module. Before doing so, in the "MyModel" module, make the "entities" package and the "javax.persistence" packages public (in the Libraries panel of the Project Properties dialog) and make the one package that you have in the "MyControler" package public too.

  2. Define the top part of the MyViewTopComponent as follows:
    public final class MyViewTopComponent extends TopComponent implements ExplorerManager.Provider {
    
        ExplorerManager controler = MyUtils.getControler();
    
        public MyViewTopComponent() {
    
            initComponents();
    
            setName(Bundle.CTL_MyViewTopComponent());
            setToolTipText(Bundle.HINT_MyViewTopComponent());
    
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
            controler.setRootContext(new AbstractNode(Children.create(new ChildFactory<Customer>() {
                @Override
                protected boolean createKeys(List list) {
                    EntityManager entityManager = Persistence.
                         createEntityManagerFactory("MyEntitiesPU").createEntityManager();
                    Query query = entityManager.createNamedQuery("Customer.findAll");
                    list.addAll(query.getResultList());
                    return true;
                }
                @Override
                protected Node createNodeForKey(Customer key) {
                    Node customerNode = new AbstractNode(Children.LEAF, Lookups.singleton(key));
                    customerNode.setDisplayName(key.getName());
                    return customerNode;
                }
            }, true)));
     
            controler.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    Customer selectedCustomer = 
                         controler.getSelectedNodes()[0].getLookup().lookup(Customer.class);
                    StatusDisplayer.getDefault().setStatusText(selectedCustomer.getName());
                }
            });
    
            JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
            row1.add(new JLabel("Customers: "));
            row1.add(new ChoiceView());
            add(row1);
    
        }
    
        @Override
        public ExplorerManager getExplorerManager() {
            return controler;
        }
    
        ...
        ...
        ...

Now run the application and you'll see the same as the image with which this blog entry started.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE