![]() |
| ||
| Set Focus on particular cell in a JTable when using TableModel 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; } |
| ||
| Re: Set Focus on particular cell in a JTable when using TableModel 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 |
| All times are GMT -4. The time now is 9:39 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC