How to make JTable on cellchange select all text

I saw some example of this, but I still cannot understand and am not able to implement it.

What I want to do is change the cell (focus), the next selected cell will have all the selected text, ready for the user to completely change it.

Any ideas on how to do this?

// update // somehow I managed to get out with the next class, but

implement this tblLayers.setDefaultEditor (String.class, new Classes.CellEditor ());

gives nothing, "Not yet supported." NOT thrown away ..

How do 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."); } } 
+4
source share
3 answers

regarding editorComponent , where do I initialize this variable?

The editorComponent variable is the DefaultCellEditor field.

Instead

 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; } 

Appendix: as shown in Edoz and shown in this full, you can selectively repeat the selectAll() queue when the mouse click starts 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; } }; 
+5
source

What I want to do is change the cell (focus), the next selected cell will have all the selected text, ready for the user to completely change it.

The decision depends on your exact requirement. JTable has a visualization tool and editor.

The output usually shows the text in the cell. If you want the text to be replaced as you type, you need to do two things:

a) change the renderer to display the text in the โ€œselectedโ€ state, so that the user knows that entering a character will delete the existing text b) change the editor to select all the text when it is called

This approach is relatively complex because you will need a separate renderer for each other data type in your table (i.e. String, Integer).

Or another approach is to automatically put each cell into edit mode when it focuses, and so you only need to change the editor to select the text.

This approach is simple as you can:

 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(); } } }; 
+11
source

Source: https://habr.com/ru/post/1386065/


All Articles