| | |
Set Focus on particular cell in a JTable when using TableModel
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
I want to implement validation in my table cells. I the value does not satisfy certain condition then the focus should remain on the same cell and the user should be asked to enter valid value.
I am using TableModel and i have tried implementing validation in the setValueAt() method some thing like this.
//**************************
setValueAt method of my table model.
public void setValueAt(Object value, int row, int col){
if(datalist.size()>row){
OutboundFieldMappingObj obj=(OutboundFieldMappingObj)datalist.get(row);
String str=null;
switch (col){
case 0:
str=Validator.getValidNumber((String)value,1,99999 9999L);
obj.setFiledName(str);
break;
case 1:
str=Validator.getValidString((String)value,128,fal se);
obj.setFiledClass(str);
break;
case 2:
obj.setSource((String)value);
break;
}}
fireTableCellUpdated(row, col);
}
But this doesn't solve the issue. In this case after entering the value when i click on some other cell then it shows me a message stating that the value is invalid but it doesn't takes the focus back to same cell. The focus is shifted to the cell where i have clicked.
Please help me out in resolving the issue.
I have added the code of my validator class at the bottom if any one needs to have a look at it.
Thanks
//**********************
Validator class
public class Validator {
public Validator() {
}
public static String getValidString(String val, int max, boolean upper) {
if (val == null || val.equals("")) {
showErrorMsg("The cell can not be left blank");
return " ";
}
if (val.length() > max) {
val = val.substring(0, max);
showErrorMsg("String value to long. Truncated to " + max +" characters.");
}
if (upper) {
val = val.toUpperCase();
}
return val;
}
public static String getValidNullableString(String val, int max, boolean upper) {
if (val == null) {
return null;
}
if (val.length() > max) {
val = val.substring(0, max);
showErrorMsg("String value to long. Truncated to " + max +" characters.");
}
if (upper) {
val = val.toUpperCase();
}
return val;
}
public static String getValidNumber(String number, int min, long max) {
try {
long num = Long.parseLong(number);
if (num < min) {
showErrorMsg("Number less than mininum value of " + min + ".");
return "" + min;
}
if (num > max) {
showErrorMsg("Number greater than maximum value of " + max +".");
return "" + max;
}
return number;
} catch (Exception x) {
showErrorMsg("Invalid Entry. Number Expected.");
return "0";
}
// return "";
}
public static boolean getValidNumber(String number) {
try {
Integer.parseInt(number);
return true;
} catch (Exception x) {
showErrorMsg("Invalid Entry. Number Expected.");
return false;
}
// return "";
}
public static void showErrorMsg(String message) {
JOptionPane.showMessageDialog(new java.awt.Frame(), message,
"Invalid Value!",
JOptionPane.ERROR_MESSAGE);
}
public static boolean validString(String str) {
if (str != null && str.length() > 0) {
return true;
}
return false;
}
I am using TableModel and i have tried implementing validation in the setValueAt() method some thing like this.
//**************************
setValueAt method of my table model.
public void setValueAt(Object value, int row, int col){
if(datalist.size()>row){
OutboundFieldMappingObj obj=(OutboundFieldMappingObj)datalist.get(row);
String str=null;
switch (col){
case 0:
str=Validator.getValidNumber((String)value,1,99999 9999L);
obj.setFiledName(str);
break;
case 1:
str=Validator.getValidString((String)value,128,fal se);
obj.setFiledClass(str);
break;
case 2:
obj.setSource((String)value);
break;
}}
fireTableCellUpdated(row, col);
}
But this doesn't solve the issue. In this case after entering the value when i click on some other cell then it shows me a message stating that the value is invalid but it doesn't takes the focus back to same cell. The focus is shifted to the cell where i have clicked.
Please help me out in resolving the issue.
I have added the code of my validator class at the bottom if any one needs to have a look at it.
Thanks
//**********************
Validator class
public class Validator {
public Validator() {
}
public static String getValidString(String val, int max, boolean upper) {
if (val == null || val.equals("")) {
showErrorMsg("The cell can not be left blank");
return " ";
}
if (val.length() > max) {
val = val.substring(0, max);
showErrorMsg("String value to long. Truncated to " + max +" characters.");
}
if (upper) {
val = val.toUpperCase();
}
return val;
}
public static String getValidNullableString(String val, int max, boolean upper) {
if (val == null) {
return null;
}
if (val.length() > max) {
val = val.substring(0, max);
showErrorMsg("String value to long. Truncated to " + max +" characters.");
}
if (upper) {
val = val.toUpperCase();
}
return val;
}
public static String getValidNumber(String number, int min, long max) {
try {
long num = Long.parseLong(number);
if (num < min) {
showErrorMsg("Number less than mininum value of " + min + ".");
return "" + min;
}
if (num > max) {
showErrorMsg("Number greater than maximum value of " + max +".");
return "" + max;
}
return number;
} catch (Exception x) {
showErrorMsg("Invalid Entry. Number Expected.");
return "0";
}
// return "";
}
public static boolean getValidNumber(String number) {
try {
Integer.parseInt(number);
return true;
} catch (Exception x) {
showErrorMsg("Invalid Entry. Number Expected.");
return false;
}
// return "";
}
public static void showErrorMsg(String message) {
JOptionPane.showMessageDialog(new java.awt.Frame(), message,
"Invalid Value!",
JOptionPane.ERROR_MESSAGE);
}
public static boolean validString(String str) {
if (str != null && str.length() > 0) {
return true;
}
return false;
}
You may want to take a look at this section of the Sun tutorial on using tables: http://java.sun.com/docs/books/tutor...html#validtext
![]() |
Other Threads in the Java Forum
- Previous Thread: Java FTP Server
- Next Thread: How to calculate the difference in two dates in java
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows






