Fixed the problems I reported having a few days ago. As a result, I am now proud to announce "SQLStringCellEditor". This class can be dropped into any module that contains a table containing a column providing an SQL string. When a double-click on the column is performed, the NetBeans SQL Editor opens, synchronized with the selected cell in the column in the table:
public class SQLStringCellEditor extends AbstractCellEditor implements
        TableCellEditor, ActionListener {

    private int clickCount = 2;
    private JButton button;
    private JTextComponent editor;
    private String value;
    private JTable table;
    private int row;
    private int col;

    public SQLStringCellEditor() {
        //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();
            //Listen for inserts and update the table:
            editor.getDocument().addDocumentListener(new DocumentListener() {
                public void insertUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
                public void removeUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
                public void changedUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
        return editor;

    //Use the value in the cell to set the button's text
    //and to define class variables holding the old value, the table, and the cell:
    public Component getTableCellEditorComponent(JTable arg0, Object val,
            boolean arg2, int row, int col) {
        this.value = val.toString();
        this.table = arg0;
        this.row = row;
        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;


TableColumn column = table.getColumnModel().getColumn(0);
column.setCellEditor(new SQLStringCellEditor());

Of course, the relevant modules need to be part of the application, such as the module providing the SQL Editor.


I have no knowledge about the SQLEditor component of Netbeans, but I think the document listener must not be changing the table directly (insertUpdate, removeUpdate, changedUpdate), It must signal the event CellEditorListener.editingStopped() using fireEditingStopped when the window is closed of if there is an ok button (or fireEditingStopped if the editor has a way to cancel editing), and return the current edited value implementing CellEditor.getCellEditorValue()

Posted by Robert on February 10, 2011 at 02:50 AM PST #

That's all true. However, in this case, I have no button where the firing of editing stopped can take place, instead whenever a change is made I want the cell to immediately be updated.

Posted by Geertjan Wielenga on February 10, 2011 at 04:12 AM PST #

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