Results 1 to 11 of 11
Thread: JTable input validation
- 06-09-2009, 09:23 AM #1
JTable input validation
I want to handle user input in a jtable column. For example there is a time input column whereby the input should always be in "##:##" format.
I am thinkin of using CellEditor but i have no idea on how to accomplish this.
what is the best way to do input validation on a jtable apart from using a JDialog??
Thanks
- 06-09-2009, 01:08 PM #2
I created a class to validate the user input in the table but now i get a null value if i select a cell and after editing the new value is not displayed. I just dont know where the mistake is
This is how i call the class Table_ValidateJava Code:import java.awt.Component; import java.text.ParseException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractCellEditor; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.TableCellEditor; import javax.swing.text.MaskFormatter; /** * * @author Munya01M */ public class Table_Validate extends AbstractCellEditor implements TableCellEditor { JFormattedTextField textField; public Table_Validate() { textField = new JFormattedTextField(); } public Object getCellEditorValue() { try { MaskFormatter mf1 = new MaskFormatter("##:##"); mf1.setPlaceholderCharacter(' '); textField = new JFormattedTextField(mf1); } catch (ParseException ex) { Logger.getLogger(Table_Validate.class.getName()).log(Level.SEVERE, "Invalid Input", ex); } return textField.getText(); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { textField.setText(String.valueOf(value)); return textField; } @Override public boolean stopCellEditing() { String s = ((String)getCellEditorValue()); if(s ==null) { fireEditingCanceled(); return false; } return super.stopCellEditing(); } }
Your opinions will be highly appreciatedJava Code:TableColumnModel columnModel = Xenon.getColumnModel(); TableColumn col1 = columnModel.getColumn(0); col1.setCellEditor(new Table_Validate());
- 06-09-2009, 02:05 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As you discuss in another thread, why don't you use regular expressions to validate that?
- 06-09-2009, 02:34 PM #4
I just got more and more confused. I would appreciate so much if you give a clue or rather an example so that i know where to start from.
Best Regards,
Manfizy
- 06-09-2009, 02:46 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
[COLOR="DarkGreen"][Ok, do you know anything about regular expressions in Java?
I'll help you, I've seen that in one of previous post you confused about that./COLOR]
- 06-09-2009, 02:54 PM #6
I really dont know anything about regular expressions in java.
May be you can give me a start and then i can research on it?We Learn Through Mistakes..,
Manfizy:rolleyes:
- 06-09-2009, 03:04 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, read the following page.
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
That's the best site you have. Then if you have any question let me know, I'll help you.
- 06-09-2009, 03:36 PM #8
I have read about the regular expressions, got alil bit confused. i found the split string method which will become useful when manipulating the user input in the table. For example, i want the user to input time in the table and this method will come in handy when adding or subtractin time since i have hours and minutes. so i have to split the two inorder to do some calculation
Anyway, my current problem now is validating what the user will input. i.e it should always be in "##:##" format. --(hh:mm)
How can i accomplish this task.?We Learn Through Mistakes..,
Manfizy:rolleyes:
- 06-10-2009, 04:52 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, just for a min forget your final requirement, that split the date and so on. What you have to do first of all is validate the date format. I'll give an example. Go through all the comments I've added there in the code.
Any questions?Java Code:import java.io.*; import java.util.regex.*; /** * * @author Eranga Thennakoon */ public class RegTest { /** * @param args the command line arguments */ public static void main(String[] args) { String strRegPtr = "..:.."; // First of all you want to have a string to compare, or a pattern to // compare with your string. strRegPtr holds that string patter. The dot // sign represent character, a letter or a numeric value. // NOTE: This is the very simple patter you can see at the start of worl in // regular expressions. Pattern pt = Pattern.compile(strRegPtr); // This is the actual patter you have. Earlier you have only the string, // character arrangment of the patter you want to build. compile() method // do the pattern build for you, from the java.util.regex package. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strInput = ""; System.out.print("Enter the date: "); try { strInput = br.readLine(); } catch(Exception ex) { ex.printStackTrace(); } // The above few line handles the user input and I hope can understand // what's going on. Matcher matcher = pt.matcher(strInput); boolean matchFound = matcher.matches(); // Matcher class compare the user input with the pattern you have. Not with // the regular expression string format you have. if(matchFound) { System.out.println("Valid format"); } else { System.out.println("Invalid format"); } } }
- 06-10-2009, 05:05 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just run the above code and see. Actually you can enter anything in the following format, all are valid.
On such cases you have to make into advance your regular expression string pattern. Just keep in mind that, we can do changes on that later.12:23 // valid date
aa:bb // valid format, but not a valid date format
- 06-10-2009, 09:04 AM #11
Hi,
Thank you very much. I found Regular expression very useful and i implemented then in my program. The only problem i have not is Loosing focus if the pattern does not match.
Here is how i did my implimentation
Java Code:public class Xenon_Table_Validate extends AbstractCellEditor implements TableCellEditor { JFormattedTextField textField; public Xenon_Table_Validate() { textField = new JFormattedTextField(); } public Object getCellEditorValue() { String strRegPtr = "..:.."; Pattern pt = Pattern.compile(strRegPtr); String _field = textField.getText(); Matcher matcher = pt.matcher(_field); boolean matchFound = matcher.matches(); if( matchFound) { System.out.println("Valid format"); //the program should loose focus and move to the next field } else { System.out.println("Invalid format"); JOptionPane.showMessageDialog(null,"Data Input Error, Plz Inter Integer value","Error",JOptionPane.ERROR_MESSAGE); //Focus should not be lost until the coreect value is input } return textField.getText(); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (isSelected){ } textField.setText((String)value); return textField; } @Override public boolean stopCellEditing() { String s = ((String)getCellEditorValue()); if(s ==null) { fireEditingCanceled(); return false; } return super.stopCellEditing(); } }
Tell me is what am doing is right and how i can deal with the focus issueWe Learn Through Mistakes..,
Manfizy:rolleyes:
Similar Threads
-
Convert to JTable, validation, more
By danieljohn in forum Java AppletsReplies: 9Last Post: 04-02-2009, 12:37 PM -
Input....in JTable
By kirtesh4u in forum AWT / SwingReplies: 0Last Post: 11-15-2008, 09:15 AM -
Input Validation
By kickflipper1087 in forum New To JavaReplies: 5Last Post: 11-03-2008, 05:47 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks