Home > Java > javaTutorial > Here are a few title options that are question-based and match the article\'s content: * How to Enforce Custom Validation Rules for JTable Cells? * How can I Create a Custom Cell Editor to Reject Inv

Here are a few title options that are question-based and match the article\'s content: * How to Enforce Custom Validation Rules for JTable Cells? * How can I Create a Custom Cell Editor to Reject Inv

DDD
Release: 2024-10-27 09:44:30
Original
282 people have browsed it

Here are a few title options that are question-based and match the article's content:

* How to Enforce Custom Validation Rules for JTable Cells?
* How can I Create a Custom Cell Editor to Reject Invalid Input in JTable?
* Beyond Type Checking: Implement

How to Reject Invalid Input in JTable Cells

In JTable, setting the column class type ensures that invalid inputs are automatically rejected with a red outline, as seen when entering double values into an Integer column. However, this behavior doesn't extend to custom validation rules, such as rejecting negative or zero values.

To replicate the automatic rejection behavior for non-positive inputs, we can create a custom cell editor that checks for validity during cell editing.

PositiveIntegerCellEditor

The following PositiveIntegerCellEditorextends the default cell editor to perform the desired validation:

<code class="java">private static 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>
Copy after login

Usage

When editing a cell in the specified column, the PositiveIntegerCellEditor will reject non-positive inputs, outline the cell in red, and prevent cell editing completion.

The provided code also includes enhancements for right-alignment and error handling during cell editing.

Addendum

The article also provides links to additional resources on cell editing validation in JTable.

The above is the detailed content of Here are a few title options that are question-based and match the article\'s content: * How to Enforce Custom Validation Rules for JTable Cells? * How can I Create a Custom Cell Editor to Reject Inv. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template