| | |
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 |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






