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);
Related
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.
Does anyone know how to cancel the editing state of a Vaadin TextField that's inside a Table? My concrete use case is that I have a table with multiple TextFields in it, that show different values when they are edited as opposed to viewed. The user clicks on a TextField, this makes the field display its editable content. If the user changes his mind and wants to stop editing the field (by pressing ESC) the field should cancel any changes the user might have made to its content and revert to displaying its view value.
So far I've been able to move the focus to another TextField but this is not what I ultimately want to achieve. Moving the focus to the parent Table would be acceptable, but doing so programmatically by calling table.focus() does not produce any visible results.
Thanks for your help,
Radu
To revert the value of any field - including TextField - before it gets committed, AbstractField#discard()
You can go into a cell in a Jtable either by clicking on it, or by going into it using cursor keys/tabs. With defaultCellEditor and a JtextField if you go in using cursor keys the caret is put at the end of the existing text field, whereas if you double click the field it will highlight the last word.
Whereas spreadsheets seem to work (such as Open Office Calc) the same way for a double clcking, but if you tab into the field and start editing the field is cleared and the first character pressed becomes the first value in the field and so on.
I want my app to work the same way as the spreadsheet. By subclassing DefaultCellEditor and adding
final Caret caret = editField.getCaret();
caret.setDot(0);
editField.setText("");
I can get it to work how I want when tabbing but it also clears the field on double click which I dont want.
So please how can I determine if cell editing has been triggered by keyboard or mouse ?
override the isCellEditable(EventObject anEvent) method too.
So that you can capture the event which is going to trigger (or not) the table edition and act the way you want
The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.
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.