• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Select JTextField value in a DefaultCellEditor

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic