Monday, August 4, 2008

Search column name in SQL DB

This query returns the complete details of a searched column in the entire database

SELECT table_name = sysobjects.name,
column_name = syscolumns.name,
datatype = systypes.name,
length = syscolumns.length
FROM sysobjects
JOIN syscolumns ON sysobjects.id = syscolumns.id
JOIN systypes ON syscolumns.xtype = systypes.xtype
WHERE sysobjects.xtype='U' and syscolumns.name like '%ColumnName%'
ORDER BY sysobjects.name, syscolumns.colid

Might be simple.. But useful at times..!!

Enabling USB through registry hack

To disable usb storage devices:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\
DWORD "start" value=4

To enable usb storage devices:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\
DWORD "start" value=3

To Disable Write protect:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\StorageDevicePolicies,"Writeprotect"=0

Override the focus traversal keys of a JTable

In a JTable, there are some predefined focus traversal keys. For example, CTRL+TAB to move out of JTable.

If that needs to be overrided with some other key combination the FocusTraversalKeySet should be replaced with a new set containing the new key combinations.

Following example shows the overriding of ForwardTraversalKey set with new KeySet containing CTRL+Z as the forward traversal key and also BackwardTraversalKey set with an EmptySet to disable all the backward traversals.


JTable table = new JTable(2,2);

TreeSet forwardSet = new TreeSet();

forwardSet.add(AWTKeyStroke.getAWTKeyStroke( KeyEvent.VK_Z,
InputEvent.CTRL_DOWN_MASK
| InputEvent.CTRL_MASK, false ) );

Set unmodifiableForwardSet =
Collections.unmodifiableSortedSet( forwardSet );

// To override the existing forward_traversal_key set
// with new unmodifiable set containing new
// key combination(CTRL+Z)
table.setFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
unmodifiableForwardSet );

// To disable all the backward traversal keys,
// an empty set can be used
table.setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
Collections.EMPTY_SET );

Add rows on tabbing to a cell beyond the last cell

JTable by defaults selects the next cell when TAB is pressed. When the current selection is last cell, pressing tab selects the first cell. But here we want a new row to be inserted and selected the next newly created cell.

JTable table = new JTable(2, 2){
private final KeyStroke tabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
AWTEvent currentEvent = EventQueue.getCurrentEvent();
if(currentEvent instanceof KeyEvent){
KeyEvent ke = (KeyEvent)currentEvent;
if(ke.getSource()!=this)
return;
// focus change with keyboard
if(rowIndex==0 && columnIndex==0
&& KeyStroke.getKeyStrokeForEvent(ke).equals(tabKeyStroke)){
((DefaultTableModel)getModel()).addRow(new Object[getColumnCount()]);
rowIndex = getRowCount()-1;
}
}
super.changeSelection(rowIndex, columnIndex, toggle, extend);
}
};

Tweaking JTable Editing

There are two client properties supported by JTable which are related to editing.

JTable.autoStartsEdit:
The value is of type Java.lang.Boolean. This property tells whether JTable should start edit when a key is types. If the value of this property is null, it defaults to Boolean.TRUE. So this feature is enabled by default in JTable.

terminateEditOnFocusLost:
The value is of type Java.lang.Boolean. This property tells whether what to do when focus goes outside of JTable. It first tries to commit the changes in editor, if can't be committed then the changes are cancelled. If the value of this property is null, it default to Boolean.FALSE. So this feature is disabled by default in JTable.

You can enable this feature as follows:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);


I suggest to make this feature to be enabled for JTable.