Hi........ How to Validate Jtable cell value that can accepts only integers? Please help...
import java.awt.*; import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import javax.swing.JList; public class JTableValidation { static JTable table; static DefaultTableModel model; public static void main(String[] args) { String[]columns={"Name","Number","Price"}; Object[][]data={ {"A",new Integer(01),new Integer(200)} ,{"B",new Integer(02),new Integer(400)} ,{"C",new Integer(03),new Integer(100)} ,{"D",new Integer(04),new Integer(800)} ,{"E",new Integer(05),new Integer(300)} ,{"F",new Integer(06),new Integer(500)} ,{"G",new Integer(07),new Integer(400)} }; model=new DefaultTableModel(data,columns); JTable table = new JTable(data, columns); JLabel headerRenderer = new DefaultTableCellRenderer(); String columnName = table.getModel().getColumnName(0); headerRenderer.setText(columnName); headerRenderer.setToolTipText("Wave"); table=new JTable(model){ public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; TableColumnModel columnModel = table.getColumnModel(); TableColumn col1 = columnModel.getColumn(1); col1.setHeaderRenderer((TableCellRenderer) headerRenderer); col1.setCellEditor(new TableEditor()); TableColumn col2 = columnModel.getColumn(2); col2.setCellEditor(new TableEditor()); table.setPreferredScrollableViewportSize(new Dimension(280,160)); JScrollPane scroll=new JScrollPane(table); JLabel label=new JLabel("JTable validation Example",JLabel.CENTER); JPanel panel=new JPanel(); panel.add(scroll); JFrame frame=new JFrame("JTable validation"); frame.add(label,BorderLayout.NORTH); frame.add(panel,BorderLayout.CENTER); frame.setSize(400,300); frame.setResizable(false); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); table=new JTable(); table.setModel(new DefaultTableModel(new Object [][][] {},new String [] {"Name", "Number","Price"}) { Class[] types = new Class [] { java.lang.String.class, java.lang.Integer.class,java.lang.Integer.class }; public Class getColumnClass(int columnIndex) { return types [columnIndex]; } }); } } class TableEditor extends AbstractCellEditor implements TableCellEditor { JTextField textField; public TableEditor() { textField = new JTextField(); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int col) { textField.setText(String.valueOf(value)); return textField; } public Object getCellEditorValue() { String s = textField.getText(); if(!s.equals("")){ try { int i = Integer.parseInt(s); } catch(NumberFormatException nfe) { System.out.println("worng Entry: " + nfe.getMessage()); JOptionPane.showMessageDialog(null,"Data Input Error, Plz Inter Integer value","Error",JOptionPane.ERROR_MESSAGE); return Integer.valueOf(0); } } else{ JOptionPane.showMessageDialog(null,"Data Input Error, Plz Inter Integer value","Error",JOptionPane.ERROR_MESSAGE); return Integer.valueOf(0); } return Integer.valueOf(s); } public boolean stopCellEditing() { Integer i = ((Integer)getCellEditorValue()); if(i==0||i>0) { fireEditingCanceled(); return false; } return super.stopCellEditing(); } }
Ads