Home  >  Article  >  Java  >  How to Implement Custom Validation in JTable Cells and Provide Visual Feedback?

How to Implement Custom Validation in JTable Cells and Provide Visual Feedback?

DDD
DDDOriginal
2024-10-31 02:42:01331browse

How to Implement Custom Validation in JTable Cells and Provide Visual Feedback?

How to Customize Input Validation in JTable Cells to Reject Specific Values

JTable by default performs input validation by rejecting values that cannot be coerced into the specified column class type. For instance, if a column's class type is set to Integer, non-integer values will be automatically rejected and the cell's outline will be set to red.

If you wish to validate cell input beyond the default type checking, you can override the setValueAt method of your table's model. However, this approach only prevents the input from being accepted and does not trigger the same visual feedback as the type checker.

Implementing Custom Validation with a Cell Editor

To achieve the same validation and visual feedback for custom validation rules, consider using a custom cell editor. In this case, you would create a subclass of DefaultCellEditor that:

  • Checks the input value against your validation rules (e.g., for positive integers).
  • Sets the isValid flag accordingly.
  • Sets the cell's border color based on the validation result.

Example: Custom Cell Editor for Positive Integers

Here is an example of a custom cell editor that validates positive integers and sets the cell's border color accordingly:

<code class="java">import javax.swing.*;
import javax.swing.table.*;

class PositiveIntegerCellEditor extends DefaultCellEditor {

    private static final Border RED = new LineBorder(Color.RED);
    private static final Border BLACK = new LineBorder(Color.BLACK);
    private JTextField textField;

    public PositiveIntegerCellEditor(JTextField textField) {
        super(textField);
        this.textField = textField;
        this.textField.setHorizontalAlignment(JTextField.RIGHT);
    }

    @Override
    public boolean stopCellEditing() {
        try {
            int v = Integer.valueOf(textField.getText());
            if (v < 0) {
                throw new NumberFormatException();
            }
        } catch (NumberFormatException e) {
            textField.setBorder(RED);
            return false;
        }
        return super.stopCellEditing();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        textField.setBorder(BLACK);
        return super.getTableCellEditorComponent(table, value, isSelected, row, column);
    }
}</code>

By setting the cell editor of the desired column to your custom implementation, you can achieve the desired validation behavior and visual feedback when users enter non-positive values.

The above is the detailed content of How to Implement Custom Validation in JTable Cells and Provide Visual Feedback?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn