X

Geertjan's Blog

  • February 10, 2011

SQLStringCellEditor

Geertjan Wielenga
Product Manager
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() {
super();
//Define the button shown in the table:
button = new JButton();
button.addActionListener(this);
button.setBorderPainted(false);
button.setHorizontalAlignment(SwingConstants.LEFT);
}
//Use the content of the editor to set the value
//of the cell:
@Override
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:
@Override
public void actionPerformed(ActionEvent e) {
getSingleSQLEditorDocument().setText(value);
}
//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() {
@Override
public void insertUpdate(DocumentEvent e) {
table.setValueAt(editor.getText(), row, col);
}
@Override
public void removeUpdate(DocumentEvent e) {
table.setValueAt(editor.getText(), row, col);
}
@Override
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:
@Override
public Component getTableCellEditorComponent(JTable arg0, Object val,
boolean arg2, int row, int col) {
this.value = val.toString();
this.table = arg0;
this.row = row;
this.col=col;
button.setText(value);
return button;
}
//Only allow editing on double-click:
public void setClickCount(int count) {
clickCount = count;
}
@Override
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
return ((MouseEvent) evt).getClickCount() <= clickCount;
}
return true;
}
}

Usage:

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.

Join the discussion

Comments ( 2 )
  • Robert Thursday, February 10, 2011

    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()


  • Geertjan Wielenga Thursday, February 10, 2011

    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.


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