Set Focus on particular cell in a JTable when using TableModel

Reply

Join Date: Nov 2008
Posts: 1
Reputation: SriSN is an unknown quantity at this point 
Solved Threads: 0
SriSN SriSN is offline Offline
Newbie Poster

Set Focus on particular cell in a JTable when using TableModel

 
0
  #1
Nov 13th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Set Focus on particular cell in a JTable when using TableModel

 
0
  #2
Nov 14th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC