Artyom Chernetsov
Artyom Chernetsov

Reputation: 1404

Custom cell editor can't accommodate text in Nimbus Look and Feel

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:

text cut

When I use default cell editor, it all looks fine:

normal look

How can I this editor look like the default editor?

Upvotes: 2

Views: 743

Answers (3)

Torsten Löhr
Torsten Löhr

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

Catalina Island
Catalina Island

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

camickr
camickr

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.

  1. you need to override getColumnClass(...) to return Integer.class so the proper renderer/editor can be used.
  2. the isCellEditable(...) method is used to determine if you can edit a cell.

Upvotes: 5

Related Questions