如何使用正则表达式验证JTable的第一列? [英] How can you validate the first column of a JTable with regex?

查看:96
本文介绍了如何使用正则表达式验证JTable的第一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,我必须验证第一列是否为IP地址.

I'm making a program, and I have to validate whether the first column is an IP-Address or not.

到目前为止,我只想出了如何找到如何过滤行的方法.但这是我将数据写入文件之前必须检查的列.

So far, I have only figured out how to find how to filter a row. But it is the column that has to be checked before I write the data to a file.

有人可以帮我吗?

推荐答案

您可以使用

You can use an InputVerifier to verify the input of the table cell. That way you don't have to verify everything after all the inputs are in, which may be a hassle if there are many incorrect inputs. With the InputVerifier You will get validation per cell input.

这里是一个例子.我不确定正则表达式是否适用于IP地址,但是我有一个最终的静态字段,您可以将其更改为起作用的正则表达式,如果不起作用的话.

Here is an example. I'm not sure if the regex is correct for IP address, but I have a final static field you can change to a regex that does work if this one doesn't.

对于这个特定的InputVerifier,如果它与正则表达式匹配的字段,或者如果该字段为空,则它将允许焦点更改,否则将出现错误对话框.

With this particular InputVerifier, if the field it matches the regex oor if the field is empty, it will allow focus change, if not you will get an error dialog.

text.matches(IP_REGEX) || text.isEmpty();

import javax.swing.DefaultCellEditor;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class TableVerifyInput {

    private static final String IP_REGEX = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$";

    public TableVerifyInput() {
        final InputVerifier verifier = getInputVerifier();
        final DefaultCellEditor editor = getTableCellEditor(verifier);

        String[] cols = {"IP address", "Column 2"};
        Object[][] data = {{null, null}, {null, null}};
        DefaultTableModel model = new DefaultTableModel(data, cols);

        JTable table = new JTable(model) {
            public TableCellEditor getCellEditor(int row, int column) {
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 0) {
                    return editor;
                } else {
                    return super.getCellEditor(row, column);
                }
            }
        };

        JFrame frame = new JFrame("Table Cell Verify");
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private InputVerifier getInputVerifier() {
        InputVerifier verifier = new InputVerifier() {

            @Override
            public boolean verify(JComponent input) {
                JTextField field = (JTextField) input;
                String text = field.getText();
                return text.matches(IP_REGEX) || text.isEmpty();
            }

            @Override
            public boolean shouldYieldFocus(JComponent input) {
                boolean valid = verify(input);
                if (!valid) {
                    JOptionPane.showMessageDialog(null, "Invalid IP address");
                }
                return valid;
            }

        };
        return verifier;
    }

    private DefaultCellEditor getTableCellEditor(final InputVerifier verifier) {
        DefaultCellEditor editor = new DefaultCellEditor(new JTextField()) {
            {
                getComponent().setInputVerifier(verifier);
            }

            @Override
            public boolean stopCellEditing() {
                if (!verifier.shouldYieldFocus(getComponent())) {
                    return false;
                }
                return super.stopCellEditing();
            }

            @Override
            public JTextField getComponent() {
                return (JTextField) super.getComponent();
            }

        };
        return editor;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TableVerifyInput();
            }
        });
    }
}

这篇关于如何使用正则表达式验证JTable的第一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆