aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Select JTextField value in a DefaultCellEditor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Select JTextField value in a DefaultCellEditor" Watch "Select JTextField value in a DefaultCellEditor" New topic
Author

Select JTextField value in a DefaultCellEditor

Guilherme Melo e Maranhao
Greenhorn

Joined: May 18, 2007
Posts: 6
Hi everybody,

I'm a having a problem with JTable. I have a JTable, created with a DefaultTableModel.

One of JTable's columns is editable. I've implemented it by setting a cellEditor in the column.

The cellEditor has a JTextField.

I need to select all the cell's value when it receives the focus (by a mouseClick or keyboard event).

When I do it in the mouseListener of the JTextField, it works, but in the FocusListener of the JTable, don't.

//MouseListener in JTextField: THIS WORKS
cellEditor.setClickCountToStart(1);
textField.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent arg0) {

int column= table.getSelectedColumn();
int row = table.getSelectedRow();
if (column == 3){

DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(column).getCellEditor();
String valorCell = (String)editor.getCellEditorValue();
TextEMax textField = (TextEMax)editor.getTableCellEditorComponent(table, valorCell, true, row, column);
textField.selectAll();
}
}

public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub

}

});

//Focus listener in JTable: THIS DOES NOT WORK
table.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent focusevent) {

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if (column == 3){

DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(column).getCellEditor();
String valorCell = (String)editor.getCellEditorValue();
TextEMax textField = (TextEMax)editor.getTableCellEditorComponent(table, valorCell, true, row, column);
textField.selectAll();

}
}

public void focusLost(FocusEvent focusevent) {
// TODO Auto-generated method stub

}

});

Why it does not select the text in focusListener?? In which listener do i have to do it??

Thanks a lot, Guilherme
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Originally posted by Guilherme Melo e Maranhao:

//Focus listener in JTable: THIS DOES NOT WORK
table.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent focusevent) {

int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if (column == 3){

DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(column).getCellEditor();
String valorCell = (String)editor.getCellEditorValue();
TextEMax textField = (TextEMax)editor.getTableCellEditorComponent(table, valorCell, true, row, column);
textField.selectAll();

}
}

public void focusLost(FocusEvent focusevent) {
// TODO Auto-generated method stub

}

});

Why it does not select the text in focusListener?? In which listener do i have to do it??


You don't really want to listen for when the table gains focus, since the cell editor is likely not active then.

If what you want is to have the cell editor select its content when the user begins to edit a cell, then you can override JTable's prepareEditor() method to do something like this:



If you want to select a cell when the table receives focus, then cell editors aren't involved. Just call something like yourTable.changeSelection(...).


bitguru blog
Guilherme Melo e Maranhao
Greenhorn

Joined: May 18, 2007
Posts: 6
Thanks a lot!
Viktor Bran
Greenhorn

Joined: Jan 17, 2008
Posts: 1
Brian Cole,

Your solution is very effective for mouse clicking, but if the user is working with the keyboard and starts typing, the first character is lost as it is used to open the editor.

Is there a way around this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Select JTextField value in a DefaultCellEditor