문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top