mahmoud
JTable Cell Validation?
3 Answer(s)      6 years and 8 months ago
Posted in : Swing AWT
Advertisement
View Answers

September 8, 2008 at 4:38 PM


Hi friend,

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={ {"Amardeep",new Integer(01),new Integer(200)}
,{"vinod",new Integer(02),new Integer(400)}
,{"Deepak",new Integer(03),new Integer(100)}
,{"Ravi",new Integer(04),new Integer(800)}
,{"Suman",new Integer(05),new Integer(300)},{"Santosh",new Integer(06),new Integer(500)},{"Sandeep",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];
}
});

}
}


September 8, 2008 at 4:40 PM


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();
}
}

-----------------------------------------

Read for more information with example at:

http://www.roseindia.net/java/example/java/swing/

Thanks


September 8, 2008 at 6:52 PM


it's not exactly what i want
i want the focus to return back to the cell
when invalid value has been entered?