Reputation: 1404
I want to validate user input in a table cell, and I use the Nimbus Look and Feel.
Here is the code of a cell editor that validates integer input: WholeNumberField
It extends JTextField
and adds input validation.
When I set it for the column it works fine, but it can't accommodate the text:
When I use default cell editor, it all looks fine:
How can I this editor look like the default editor?
Upvotes: 2
Views: 743
Reputation: 131
I found that putting the following to my Custom Cell Editor constructor solved the problem for me:
Border border = UIManager.getBorder("Table.cellNoFocusBorder");
if (border != null) {
setBorder(border);
}
My Editor extends JTextField.
Upvotes: 5
Reputation: 7126
If you get an instance of the TableCellEditor
from getDefaultEditor(Object.class)
, it should already be a component that you can validate like in your example.
Upvotes: 2
Reputation: 324108
The WholeNumberField is old code. If you really want to do custom validation then you should be using a DocumentFilter.
However, in this case, there is no need to create a custom editor. JTable already supports an editor to validate numbers. You just need to override the isCellEditable(...)
method of the JTable or the TableModel to return Integer.Class
and the proper renderer and editor will be used.
Edit: Just noticed my suggestion is incorrect.
getColumnClass(...)
to return Integer.class so the proper renderer/editor can be used. isCellEditable(...)
method is used to determine if you can edit a cell.Upvotes: 5