File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Select JTextField value in a DefaultCellEditor

 
Guilherme Melo e Maranhao
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...).
 
Guilherme Melo e Maranhao
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
 
Viktor Bran
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic