Share on Google+Share on Google+

mahmoud
JTable Cell Validation?
3 Answer(s)      8 years ago
Posted in : Swing AWT

Ads

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?









Tutorials   
Java Spring Hibernate Struts Training Change JPanel background color from mouse entered/exit event JTable Pagination Jcombobox JAVA SWING how can i display a pdf file in a jtextarea Crop Image Code in Applet Crop Image Code in Applet Image Manipulation in Swing AWT Applet Ho to get virtual key code. how to show set of images continously using JScrollpane in java how to show set of images continously using JScrollpane in java How to connect on screen keyboard to system. How to connect on screen keyboard to system. query java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings Download. how to open one Jframe from main method call Urgent requirement display shapes i am getting an error in adding a menu item to a menu. I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this Sliding window protocol how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider Jbutton in JTable cells program code java textfield problem swing retrieve data from database in java swing form using prev n next buttons retrieve data from database in java swing form using prev n next buttons How to close a frame on action event of a button from a different class ? searching the data swings for webnms Jpanel allignment pass data from java frame to file code for traffic control system simulation Bank Management System how to maKE Jcombox editable after saveing value init. i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled. i havei) a jtable with few cloumns and few rows andii) one button. when place the cursor in any of the table's column cell. i need to enable the button.by default the button should be disabled. PDF Comparator i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox. issue on jcombobox

Ads

 
Advertisement null

Ads