Here's a cool database-display component I found out about today. Via the code below, you'll first end up with this window:

Then you can use the combo box to set a database connection, after which you click the button, which results in this display:

Here's all the code:


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
import org.netbeans.api.db.explorer.ConnectionManager;
import org.netbeans.api.db.explorer.DatabaseConnection;
import org.netbeans.modules.db.dataview.api.DataView;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;

@TopComponent.Description(preferredID = "DemoDataView",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "")
@ActionReference(path = "Menu/Window" /\*, position = 333 \*/)
@TopComponent.OpenActionRegistration(displayName = "#CTL_MyDataViewAction",
preferredID = "DemoDataView")
public class DemoDataView extends TopComponent {

    DatabaseConnection dbconn = null;

    public DemoDataView() {

        final JComboBox dbconnComboBox = new JComboBox();
        final JButton dbconnButton = new JButton("Display");
        final JScrollPane dbscrollPane = new JScrollPane();
        setDisplayName("Database Content");

        //Connect to combo box to the Connection Manager:
        DatabaseExplorerUIs.connect(dbconnComboBox, ConnectionManager.getDefault());

        //Set the connection based on the combo box:
        dbconnComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object selected = dbconnComboBox.getSelectedItem();
                if (selected instanceof DatabaseConnection) {
                    dbconn = (DatabaseConnection) selected;

        //Create a data view, with hard coded sql statement:
        dbconnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DataView dv = DataView.create(dbconn, "select \* from APP.CUSTOMER", 100);
                List<Component> createComponents = dv.createComponents();
                for (Component component : createComponents) {

        //Put all the components on the window:
        JPanel panel = new JPanel(new MigLayout());
        panel.add(dbconnComboBox, "wrap");
        panel.add(dbconnButton, "wrap");

        setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER);



Required modules:

  • MigLayout
  • Database DataView (impl dep)
  • Database Explorer (impl dep)
  • Lookup API
  • Settings API
  • UI Utilities API
  • Utilities API
  • Window System API


I have a slightly different problem, I've been searching online for answers, can't seem to figure it out.
I have a data entry form (and mySQL table) where the user can add data to the table. Basically there are tables "clients" and "cases". When entering data into the "cases" table, I would like to restrict entry of the "client" field to only "client(full_name)" taken from the "clients" table. I am attempting to do this with a comboBox, but cannot get the data from "clients" table to display. Can you help?
PS - I'm a software engineering student working on an assignment in Netbeans.

Posted by Emmanuel on June 27, 2011 at 10:31 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2015