Geertjan's Blog

  • February 6, 2011


Geertjan Wielenga
Product Manager
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:

package org.my.dataview;
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.api.db.explorer.support.DatabaseExplorerUIs;
import org.netbeans.modules.db.dataview.api.DataView;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
@TopComponent.Description(preferredID = "DemoDataView",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.my.dataview.DemoDataView")
@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

Join the discussion

Comments ( 1 )
  • Emmanuel Tuesday, June 28, 2011

    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.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.