问题
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.
Can somebody help me?
回答1:
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.
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.
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();
}
});
}
}
回答2:
Just retrieve the column data and apply an regular expression on it to check for a valid IP:
String regex = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$";
String ip = (String)table.getValueAt(row, column);
boolean isValidIP = ip.matches(regex);
来源:https://stackoverflow.com/questions/22015273/how-can-you-validate-the-first-column-of-a-jtable-with-regex