How to change default stop edit behavior in jtable - java

When you edit a cell in an jtable the value is made permanent only if the focus is changed or if one hits enter (for example if you change the cell value from "abc" to "xyz" and close the window the edit is lost). How do i make the edit permanent when the user stops editing?
I guess we have to update the cell value as the cell value is being editted but i am not sure how. Any help is appreciated!
Thanks in advance!

// tell JTable to stop editing and save any changes when the table
// loses focus. This means edits will be saved when clicking on
// another component, eg: button.
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

I would setup an ActionListener on the cell that saves the value of the cell to a variable after every keystroke (assuming the cell has focus). Then no matter what happens to the windows this variable will still contain whatever was currently in the cell. Pretty simple.

Related

Disable CTRL+C on JTable (after duoble click on a cell)

I've been asked to edit a java desktop application developed by others. I've never worked on Java desktop application before.
There's a JTable and I have to prevent the user from doing CTRL+C and copy the value of a cell. I did it.
The problem is that when the user double clicks a cell and the cursor appears (as you can see from the pic)
I'm not able to prevent the user to do CTRL+C. How can I do it?
The problem is that when the user double clicks a cell and the cursor appears
Focus is no longer on the JTable. It is on the editor of the cell which happens to be a JTextField. So you need to remove the copy functionality of the text field.
You do this by removing the Key Binding for the "Control C" on the text field:
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Object.class);
JTextField textField = (JTextField)editor.getComponent();
InputMap im = textField.getInputMap();
im.put(KeyStroke.getKeyStroke("control C"), "none");
You will need to do this for each editor type you have in your table. For example if you have Integer values then you would need to get the editor for the Integer.class and remove its key binding as well.
Note, the same approach can be used for the table, except you use the following InputMap for the table:
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
Take away their keyboard!
Okay, that may be problematic. If you don't need the users to edit the table, you could try making the table non-editable. Rather than regurgitate one of many examples on this, I'll leave you this guide on doing it and a StackOverflow question on how to Disable user edit in JTable .
Alternatively, you could look into overriding the keybindings. I've never done that personally, so I leave you a link or two and a StackOverflow question on JTable Key Bindings .
You could also do something clever by accessing the System's Clipboard from java.awt.toolkit. Maybe have it that whenever a user is editing a cell, their clipboard is set to an empty string or (if you don't want to screw over people editing cells but not trying to copy them) you could combine that with the keybindings so they think they're copying data but they end up pasting "Sorry. Copying is not allowed in this application."
Hope something here works.

Java: break focus cycle in jtable

In my JTable, I want to edit all relevant cells like in Excel.
I implemented that ENTER stops editing the cell and transfers focus to the next cell. However, when I hit ENTER in the last cell, the focus cycle makes me jump to the fist cell. But I want to continue outside the table and set focus to a JButton.
How can I break this cycle?
JTable is not a spreadsheet, but setCellSelectionEnabled() should allow you to proceed. Use setDefaultButton() as required, for example.
Addendum: In addition to setDefaultButton(), you can evoke any desired action in your custom Action, as shown here.

How can I get table cell value as "xyz" WITHOUT hit the Enter or Tab key?

I found out if you editing a TableViewer cell without hit the Enter or Tab key. Then the new cell value won't take effect. For example, a table cell value is "abc" and you change it to "xyz". Then when you read that table cell value, it still "abc". You MUST hit the Enter or
Tab key, then you will get that table cell value as "xyz".
How can I force that table cell value as "xyz" WITHOUT hit the Enter or Tab key?
Thanks very much in advance!
i suggest you check the tableviewer api.
if it was a standard JTable you simply use this:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Temporally, i make a losing focus by set focus from current view to another view in RCP :). It's not official solution but effect.

JTable custom cell renderer focus problem

I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.
Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit Enter. So If I click the 'Save & Close' button, it'll show me an error that password field is empty.
Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. When I had to add more and more stuff, I changed it to a table like this.
For now I have put a label to let people know that they should hit the ENTER.. This is not nice. Another big issue. Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. In Windows system look, there's not much visible difference whether the field is focused or not.
I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.
So your problem is, that you are still editing the cell. So you have to stop the editing and then the cell will be changed.
At your button you can get the cell who is being edited with
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with
if(cellEditor!=null){
cellEditor.stopCellEditing();
}
and then you can save the value
Tell the table to automatically commit when losing focus:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Enter value in JTable cell and click OK does not register value

I have a JTable which has certain cells editable. If the user enters in a value into an editable cell and then clicks the OK button on my form, the code:
// Get the value in the editable cell
String value = model.getValueAt(row, column);
does not pick up the entered value.
The only way the entered value is picked up by the above code is once the user has pressed enter after typing in the value or clicking in another cell for the value to be "pushed in".
Does someone know what code I could write that would "push" the value in when they click the OK button?
The JTable doesn't know when you are finished typing until you use "Enter" or tab to a new cell.
The value you are typing is not saved in the TableModel until you do one of the above.
A couple of solutions:
When you create the table you can use:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Or, in the ActionListener you can add:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
See Table Stop Editing for more information on above suggestions.
Here is a simple code whice will help you.
if(jTable.isEditing){
jTable.stopCellEditing();
}

Categories