i have seen some example of doing it but i still can't understand and not able to implement it.
What i want to do is on cell change (focus), the next selected cell will have all the text selected, ready for user to totally change it..
Any ideas on how to do it?
//update// somehow i managed to come out with the following class but
implement this
tblLayers.setDefaultEditor(String.class, new Classes.CellEditor());
yields nothing, the "Not supported yet." is NOT thrown..
how should I solve this problem?
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
public class CellEditor extends JTextField implements TableCellEditor {
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// final JTextField ec = (JTextField) editorComponent;
//
// ec.setText((String) value);
//
// // selectAll, so that whatever the user types replaces what we just put there
// ec.selectAll();
//
// SwingUtilities.invokeLater(new Runnable() {
//
// public void run() {
// // make the component take the keyboard focus, so the backspace key works
// ec.requestFocus();
//
// SwingUtilities.invokeLater(new Runnable() {
//
// public void run() {
// // at this point the user has typed something into the cell and we
// // want the caret to be AFTER that character, so that the next one
// // comes in on the RHS
// ec.setCaretPosition(ec.getText().length());
// }
// });
// }
// });
// return editorComponent;
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getCellEditorValue() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isCellEditable(EventObject anEvent) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean shouldSelectCell(EventObject anEvent) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean stopCellEditing() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void cancelCellEditing() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void addCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void removeCellEditorListener(CellEditorListener l) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
What i want to do is on cell change (focus), the next selected cell will have all the text selected, ready for user to totally change it..
The solution depends on your exact requiement. A JTable has a renderer and an editor.
A render generally just shows the text in the cell. If you want the text replaced when you start typing then you need to do two things:
a) change the renderer to display the text in a "selected" state so the user knows that typing a character will remove the existing text b) change the editor to select all the text when it is invoked
This approach is relatively difficult because you will need a custom renderer for each different data type in your table (ie. String, Integer).
Or another approach is to automatically place each cell in editing mode when it gets focus and therefore you only need to change the editor to select the text.
This approach is easy as you can just do:
JTable table = new JTable(data, columnNames)
{
// Place cell in edit mode when it 'gains focus'
public void changeSelection(
int row, int column, boolean toggle, boolean extend)
{
super.changeSelection(row, column, toggle, extend);
if (editCellAt(row, column))
{
Component editor = getEditorComponent();
editor.requestFocusInWindow();
((JTextComponent)editor).selectAll();
}
}
};
regarding
editorComponent
, where do I initialize this variable?
The variable editorComponent
is a field of DefaultCellEditor
.
Instead of
class CellEditor extends JTextField implements TableCellEditor
consider
class CellEditor extends DefaultCellEditor
Then you can do something like this:
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
JTextField ec = (JTextField) editorComponent;
if (isSelected) {
ec.selectAll();
}
return editorComponent;
}
Addendum: As suggested by @ Edoz and illustrated in this complete example , you can selectively re-queue the selectAll()
when a mouse-click initiates editing.
JTable table = new JTable(model) {
@Override // Always selectAll()
public boolean editCellAt(int row, int column, EventObject e) {
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor == null || !(editor instanceof JTextComponent)) {
return result;
}
if (e instanceof MouseEvent) {
EventQueue.invokeLater(() -> {
((JTextComponent) editor).selectAll();
});
} else {
((JTextComponent) editor).selectAll();
}
return result;
}
};
this is how i have done it
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected,boolean hasFocus,int row,int column)
{
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if(isSelected)
{
Component editor = table.getEditorComponent();
if (editor instanceof JTextComponent)
{
final JTextComponent jtc = (JTextComponent) editor;
jtc.setCaretPosition(0);
jtc.selectAll();
jtc.getCaret().setSelectionVisible(true);
jtc.getCaret().setVisible(true);
jtc.getCaret().setBlinkRate(1000);
}
}
return this;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.