Results 1 to 11 of 11
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post 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

  2. #2
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    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

    Java 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();
    
    }
    }
    This is how i call the class Table_Validate

    Java Code:
    TableColumnModel columnModel = Xenon.getColumnModel();
     TableColumn col1 = columnModel.getColumn(0);
     col1.setCellEditor(new Table_Validate());
    Your opinions will be highly appreciated

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    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

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    [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]

  6. #6
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    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:

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    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:

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    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");
            }
        }
    
    }
    Any questions?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Just run the above code and see. Actually you can enter anything in the following format, all are valid.

    12:23 // valid date
    aa:bb // valid format, but not a valid date format
    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.

  11. #11
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    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 issue
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

Similar Threads

  1. Convert to JTable, validation, more
    By danieljohn in forum Java Applets
    Replies: 9
    Last Post: 04-02-2009, 12:37 PM
  2. Input....in JTable
    By kirtesh4u in forum AWT / Swing
    Replies: 0
    Last Post: 11-15-2008, 09:15 AM
  3. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 05:47 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •