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

    Post Strings and delimeters

    HI people,
    I am trying to get user input in a table in the form 00:00
    with this in mind, the input has to be returned as a string for it to be in that format. Am thinking of using delimeters to seperate the two values and if the user attempts to input int or double an error shoud be displayed.

    Anyone with an idea how i can do this?

    Your opinions are highly appreciated

    Best regards,
    manfizy

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You can use a Pattern and a Matcher to achieve this. With the correct regular expression you can make sure you always get the input you want.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  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

    Default

    I suggest to use regular expressions too in this case. It's much much better/safer than delimiters and stuff.

    How did you going to have value type after separation, when you displaying the error message?

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

    Post

    Am still a newbie in java and so am just trying things out - trial and error you know..
    I tried reading the Pattarn and matcher class but i couldnt understand a thing lol!
    All i want is to make sure as the user edits the table, the table can only accept inputs in "00:00" format. Apparently the input is time, and thats why i want it that way.

    @Erlanga, i didnt understand your opinion/question please...

    Best Regards,
    Manfizy

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    Hi,
    Are you using Java GUI or is it a HTML page?.Because ,if you use swings for user entry then we are having "javax.swing.JFormattedTextField" class in java.This class will allow the user to enter in the required format.Inside this "JFormattedTextField",they have internally used Patterns.

    -Regards
    Ramya
    Ramya:cool:

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Search for some info on regular expressions. A simple one to use would be ^\d\d:\d\d$ but you could also be clever and restrict it to valid times using a re as well.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Post

    @Ramya - am using Netbeans to build my GUI. Already i have a table that is editable and the user can input some data on it. Your idea seems good. With my little knowledge in java i know that if am to use JFormatTextField i have to listen for editing changes or something..
    Just shade somelight by a small example that listens to changes and then uses JFormatTextField.

    I hope you dont mind my dumbass questions but am just eager to learn to java.

    Thanks

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    Hi ,
    I have taken a sample code from the net and edited according to your need.Just gothru.

    This code will accept only number in "00:00" format.Just gothru the highlighted portion and study.


    Java Code:
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.text.MaskFormatter; 
     
    public class SimpleFTF  {
    
      public static void main(String args[]) throws ParseException  {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
    
       [B] MaskFormatter mf1 = new MaskFormatter("##:##");
        mf1.setPlaceholderCharacter('_');
        JFormattedTextField ftf1 = new JFormattedTextField(mf1);[/B]    content.add(ftf1);
    
       
        f.setSize(300, 100);
        f.setVisible(true);
    
      }
    }
    Ramya:cool:

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

    Post

    Hi, thank you for your reply
    This seems to be the solution to my problem but how can i impliment this on a table such that this happens only when the cells in the table are being edited??

  10. #10
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    You could of course, use the String.split function.
    I die a little on the inside...
    Every time I get shot.

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

    Post

    I did alil bit of research on how i can implement the maskFormatter method in a table and this is what i could come up with. BUT i still get some errors.
    Could you please tell me exactly where i am making a mistake in my code


    Java Code:
      public void focusGained(FocusEvent e) {
         try{
            int rows = table1.getRowCount();
            MaskFormatter mf1 = new MaskFormatter("##:##");
            mf1.setPlaceholderCharacter(' ');
            JFormattedTextField ftf1 = new JFormattedTextField(mf1);    table1.add(ftf1);
         for (int i =0;i<rows;i++){
            table.setValueAt(ftf1 , i, 0);    
              }
         }catch (NumberFormatException nfe){
          System.out.println("invalid input");
           }
        }
    
        public void focusLost(FocusEvent e) {
            focusGained(e);
        }
    Apparently the formatting should only be in the first column cells. The rest should remain the way they are.

    Best Regards,
    Manfizy

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

    Post

    I did a lil bit of research on how i can implement the masFormatter method in a JTable and this is what i came up with. BUT i still get some errors in my code.
    Couls you please tell me exactly where i am making a mistake in my code??

    Java Code:
       public void focusGained(FocusEvent e) {
         try{
            int rows = table1.getRowCount();
            MaskFormatter mf1 = new MaskFormatter("##:##");
            mf1.setPlaceholderCharacter(' ');
            JFormattedTextField ftf1 = new JFormattedTextField(mf1);    table1.add(ftf1);
         for (int i =0;i<rows;i++){
            table1.setValueAt(ftf1 , i, 0);    
              }
         }catch (NumberFormatException nfe){
          System.out.println("invalid input");
           }
        }
    
        public void focusLost(FocusEvent e) {
            focusGained(e);
        }
    Apparently the formatting should be only in the first column cells and the rest should remain the way they are.

    Best Regards,
    Manfizy

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

    Post

    I did a research on how i can implement masFormatter method in Jtable and came up with the code below THOUGH i still get some errors in my code.
    could you please tell me where i could be making a mistake in my code.

    Java Code:
       public void focusGained(FocusEvent e) {
         try{
            int rows = table1.getRowCount();
            MaskFormatter mf1 = new MaskFormatter("##:##");
            mf1.setPlaceholderCharacter(' ');
            JFormattedTextField ftf1 = new JFormattedTextField(mf1);    table1.add(ftf1);
         for (int i =0;i<rows;i++){
            table1.setValueAt(ftf1 , i, 0);    
              }
         }catch (NumberFormatException nfe){
          System.out.println("invalid input");
           }
        }
    
        public void focusLost(FocusEvent e) {
            focusGained(e);
        }
    Apparently the formatting should only be in the first column cells and the rest should remain the same.

    Best Regards
    manfizy:confused:

Similar Threads

  1. The java Strings
    By greenteachacha in forum Jobs Discussion
    Replies: 8
    Last Post: 04-16-2009, 05:08 AM
  2. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  3. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  4. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 09:10 PM
  5. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 02: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
  •