Geertjan's Blog

  • February 8, 2011

NetBeans SQL Editor as TableCellEditor

Geertjan Wielenga
Product Manager
The original NDVis has a JFrame providing a query builder. Query strings are created and then mapped to colors. As you can see, below, when you click on the column of the table where the query string is defined, another JFrame opens, where you have more space to define the query:

However, there's no syntax coloring, no code completion, and no way to test whether those queries actually work, until later when you perform the color mapping. So, while porting the application to the NetBeans Platform, I'm building in a nice new feature where, when you click on the query column, the NetBeans SQL Editor will open and display the currently selected query from the table:

Once it actually works, this will be pretty cool. You'll define your query in the SQL Editor (and you'll even be able to run the query to see that it works) and then the column in the table will be updated.

I discovered this interesting utility call that opens the SQL Editor, where you pass in a DatabaseConnection, an SQL string, and whether the SQL should be executed immediately (I believe that's what it's for) or not:

SQLEditorSupport.openSQLEditor(connection, sql, true)

Getting it completely right is a bit of a problem though. Would be best to integrate with the save functionality, except that, as far as I know, extending the Lookup of a TopComponent over which you have no control (i.e., the SQL Editor) isn't possible.

Spent some time on this today and still haven't got it working correctly.

public class SQLCellEditor extends AbstractCellEditor implements
TableCellEditor, ActionListener {
private int clickCount = 2;
JButton button;
JTextComponent editor;
String value;
public SQLCellEditor() {
//Define the button shown in the table:
button = new JButton();
//Use the content of the editor to set the value
//of the cell:
public Object getCellEditorValue() {
return editor.getText();
//When the action is invoked, i.e., button clicked,
//set the text in the editor to the value of the cell:
public void actionPerformed(ActionEvent e) {
//Open one SQL Editor document only, regardless
//of how many times the action is invoked:
private JTextComponent getSingleSQLEditorDocument() {
if (editor == null) {
SQLEditorSupport.openSQLEditor(null, value, true);
editor = EditorRegistry.lastFocusedComponent();
return editor;
//Use the value in the cell to set the button's text
//and to define a class variable holding the old value:
public Component getTableCellEditorComponent(JTable arg0, Object val,
boolean arg2, int row, int col) {
value = val.toString();
return button;
//Only allow editing on double-click:
public void setClickCount(int count) {
clickCount = count;
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
return ((MouseEvent) evt).getClickCount() >= clickCount;
return true;

Hope someone can help. Right now, changes in the editor do not result in changes in the cell in the table. Using a DocumentListener, I am informed about the inserts, but then I do not know how to get those changes back into the cell in the table.

Join the discussion

Comments ( 1 )
  • guest Thursday, August 18, 2011

    The table asks for the new value of the editor after editing stops by calling TableCellEditor.getCellEditorValue(), so return the new value here.

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