Hey
Hey

Reputation: 95

JTable get value from cell when is not submitted

I would like to get value from cell when its is no submitted (cell is in edit mode) - "real time"; Is it possible?

I tried this but it is working only if i submit data - press enter

int row = jTable.getSelectedRow();
int col = jTable.getSelectedColumn();
String cellValue = jTable.getValueAt(row, col).toString();

I want to get on keypress cell value without exiting it, get this text real time while typing

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
          @Override
          public boolean dispatchKeyEvent(KeyEvent e) {

              int row = jTable.getSelectedRow();
              int col = jTable.getSelectedColumn();


              if (e.getID() == KeyEvent.KEY_RELEASED) {
                  if (jTable.isEditing())
                      jTable.getCellEditor().stopCellEditing();
                  String cellValue = (jTable.getValueAt(row, col)!=null) ? jTable.getValueAt(row, col).toString() : "";
                  System.out.println(cellValue);
              }}

jTable.getCellEditor().stopCellEditing() - cause ugly in/out animation while typing

@camickr Sorry for the confusion. Your solution is ok. I just needed to add jTable.editCellAt(row, col); to get back into edit mode.

Thanks again

Upvotes: 0

Views: 72

Answers (1)

camickr
camickr

Reputation: 324157

cell is in edit mode

The editing must be stopped before the value is saved to the model.

The easiest way to do this is to use:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

when you create the table.

Now when you click on the button to do your processing the table loses focus so the data is saved.

Check out Table Stop Editing for more information.

Upvotes: 1

Related Questions