Results 1 to 13 of 13
  1. #1
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default Verifying data and remembering it in JTextfield at the same time

    Hi!

    I have a problem with remembering data from textfield. I'm working with textfields with numbers and would like to verify that user typed in correct type of data and at the same time i'd like to remember the input. I only managed to solve a verifying part of a problem (is input a number?).
    How could I get the variable (String) text from a code to be seen out of the method verify? I have loads of textfields to check, so this method will be called numerous times.
    Also, do you think it's better that method verify wouldn't be nested (because it would be called so often)? Could you help me with rearranging the code so that method verify would be called from method something?
    Thanks for helping me!

    Code:

    public somethig..... {

    JTextField test = newJTextField(5);
    InputVerifier verifier = new InputVerifier() {

    public boolean verify(JComponent input) {

    final JTextComponent source = (JTextComponent) input;
    String text = source.getText();
    try {
    int num = Integer.valueOf(text);
    return true;
    }
    catch (NumberFormatException nfe){
    ......did something
    return false;
    }
    }
    };
    test.setInputVerifier(verifier);
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    How could I get the variable (String) text from a code to be seen out of the method verify?
    Define a variable outside of the verify method to hold the value. Assign it a value in the method

    Please use code tags when posting code: BB Code List - Java Forums

  3. #3
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    I tried that already but the program was complaining that it should be declared final. Now I defined it in class declarations and program doesn't complain. But variable text now doesn't update in method something. After line test.setInputVerifier(verifier); i get text = null even though it isn't and method verify still works correctly. Any idea?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    I'm not sure what you are saying, You'll have to post your code

  5. #5
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    I'm posting the code. When you sign in dimensions in first window and click button Ok, new window appears where your input should be written. But it says null null.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.text.JTextComponent;
    
    
    public class prvaDelovnaVerzija extends JFrame {
    
       JTextField stolpciField, vrsticeField;
       JLabel stolpciLabel, vrsticeLabel;
       JButton naprejButton;
       Container container;
       String text;
       String vrst ;
       String stolp ;
    
       public prvaDelovnaVerzija() {
          super("Window");
          container = getContentPane();
          container.setLayout(new GridLayout(4,1));
    
          vrsticeLabel = new JLabel ("Number of rows: ");
          stolpciLabel = new JLabel ("Number of columns: ");
          vrsticeField = new JTextField(3);
          stolpciField = new JTextField(3);
    
          vrsticeField.setHorizontalAlignment(vrsticeField.RIGHT);
          stolpciField.setHorizontalAlignment(stolpciField.RIGHT);
       }
    
       public void oknoVpisDimenzij() {
    
          JPanel navodiloPane = new JPanel (new FlowLayout(FlowLayout.LEFT));
          JLabel navodiloLabel = new JLabel ("Insert matrix dimensions: ");
          navodiloPane.add(navodiloLabel);
    
          JPanel vrsticePane = new JPanel(new FlowLayout());
          vrsticePane.add(vrsticeLabel);
          vrsticePane.add(vrsticeField);
    
          JPanel stolpciPane = new JPanel(new FlowLayout());
          stolpciPane.add(stolpciLabel);
          stolpciPane.add(stolpciField);
    
          JPanel buttonPane = new JPanel(new FlowLayout());
          JButton naprejButton = new JButton("Ok");
          buttonPane.add(naprejButton);
    
          container.add(navodiloPane);
          container.add(vrsticePane);
          container.add(stolpciPane);
          container.add(buttonPane);
    
          /*Verifier */
    
          InputVerifier verifier = new InputVerifier() {
    
             public boolean verify(JComponent input) {
    
                final JTextComponent source = (JTextComponent) input;
                text = source.getText();
                try {
                   int num = Integer.valueOf(text);
                   if (num>0){
                      return true;
                   }
                   else {
                      napakaDimenzije(source);
                      return false;
                   }
                }
                catch (NumberFormatException nfe){
                   napakaDimenzije(source);
                   return false;
                }
             }
          };
    
    
          vrsticeField.setInputVerifier(verifier);
          vrst = text;
          stolpciField.setInputVerifier(verifier);
          stolp = text;
    
    
         naprejButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               JFrame frame = new JFrame();
               JLabel testni = new JLabel (""+ stolp + " " +  vrst);
               frame.add(testni);
               frame.pack();
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
            }
         });
    
          setLocationRelativeTo(null);
          setSize(200, 200);
          setVisible(true);
       }
    
       public void napakaDimenzije(JTextComponent source){
          JOptionPane.showMessageDialog(source, "Dimension must be postitive integer!", "Error Dialog",
                                        JOptionPane.ERROR_MESSAGE);
       }
    
    
       public static void main(String args[]) {
          prvaDelovnaVerzija test = new prvaDelovnaVerzija();
          test.oknoVpisDimenzij();
          test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    Where do you put values in the two variables: stolp + " " + vrst

    The only place I see is where you set them, they are set to the variable: text which is null.

    What values should those two variables have?
    Where do those values come from?

    Add some printlns to your code to show when the variables are set:

    System.out.println("1text=" + text);

  7. #7
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Line 'vrst = text' is the only one where I set variable 'vrst'. I want that variable 'vrst' would have value of the variable 'text' which is set in method verify.
    So, you insert your data in textfield, with line 'vrsticeField.setInputVerifier(verifier);' method verify is called and in that method variable text is set with
    'text = source.getText();'. I would like for variable 'vrst' to have that value.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    variable 'vrst' would have value of the variable 'text' which is set in method verify.
    You need to debug your code: Add some printlns to your code to show when the variables are set and what the value is:

    System.out.println("1text=" + text);

    If you add several of these to your program at all the places where 'text' is used you will see the problem.

  9. #9
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    I added print in a code. After a line 'vrsticeField.setInputVerifier(verifier);' text is still null and pops up in the console before you even typed anything in textfield. I meant that with that line verify method is called, but it appears it's not. I'm confused now. Everywhere in method oknoVpisDimenzij() text is null except in verify method. I have no idea where i could get the value i want.


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.text.JTextComponent;
    
    
    public class prvaDelovnaVerzija extends JFrame {
    
       JTextField stolpciField, vrsticeField;
       JLabel stolpciLabel, vrsticeLabel;
       JButton naprejButton;
       Container container;
       String text;
       String vrst ;
       String stolp ;
    
       public prvaDelovnaVerzija() {
          super("Window");
          container = getContentPane();
          container.setLayout(new GridLayout(4,1));
    
          vrsticeLabel = new JLabel ("Number of rows: ");
          stolpciLabel = new JLabel ("Number of columns: ");
          vrsticeField = new JTextField(3);
          stolpciField = new JTextField(3);
    
          vrsticeField.setHorizontalAlignment(vrsticeField.RIGHT);
          stolpciField.setHorizontalAlignment(stolpciField.RIGHT);
       }
    
       public void oknoVpisDimenzij() {
    
          JPanel navodiloPane = new JPanel (new FlowLayout(FlowLayout.LEFT));
          JLabel navodiloLabel = new JLabel ("Insert matrix dimensions: ");
          navodiloPane.add(navodiloLabel);
    
          JPanel vrsticePane = new JPanel(new FlowLayout());
          vrsticePane.add(vrsticeLabel);
          vrsticePane.add(vrsticeField);
    
          JPanel stolpciPane = new JPanel(new FlowLayout());
          stolpciPane.add(stolpciLabel);
          stolpciPane.add(stolpciField);
    
          JPanel buttonPane = new JPanel(new FlowLayout());
          JButton naprejButton = new JButton("Ok");
          buttonPane.add(naprejButton);
    
          container.add(navodiloPane);
          container.add(vrsticePane);
          container.add(stolpciPane);
          container.add(buttonPane);
    
          /*Verifier */
    
          InputVerifier verifier = new InputVerifier() {
    
             public boolean verify(JComponent input) {
    
                final JTextComponent source = (JTextComponent) input;
                text = source.getText();
                System.out.println(text);
                try {
                   int num = Integer.valueOf(text);
                   if (num>0){
                      return true;
                   }
                   else {
                      napakaDimenzije(source);
                      return false;
                   }
                }
                catch (NumberFormatException nfe){
                   napakaDimenzije(source);
                   return false;
                }
             }
          };
    
    
          vrsticeField.setInputVerifier(verifier);
          System.out.println("text1: " + text);
          vrst = text;
          stolpciField.setInputVerifier(verifier);
          stolp = text;
    
    
         naprejButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               JFrame frame = new JFrame();
               JLabel testni = new JLabel (""+ stolp + " " +  vrst);
               frame.add(testni);
               frame.pack();
               frame.setLocationRelativeTo(null);
               frame.setVisible(true);
            }
         });
    
          setLocationRelativeTo(null);
          setSize(200, 200);
          setVisible(true);
          System.out.println("text2: " + text);
       }
    
       public void napakaDimenzije(JTextComponent source){
          JOptionPane.showMessageDialog(source, "Dimension must be postitive integer!", "Error Dialog",
                                        JOptionPane.ERROR_MESSAGE);
       }
    
    
       public static void main(String args[]) {
          prvaDelovnaVerzija test = new prvaDelovnaVerzija();
          test.oknoVpisDimenzij();
          test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    where i could get the value i want.
    From the one place it exists, in the text fields.



    I added printlns in 4 places, numbered 0 to 3:
    1text=null
    2text=null
    0text=11 // in verify
    0text=22 // in verify
    3text=22 // in actionListener method

  11. #11
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    I did a String table which is filled in verify method with 'text' values. I print it in actionListener method and it seems I finally got the values I want. Thanks for helping!

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,615
    Rep Power
    25

    Default

    Not sure doing it in the verify method is best. Once verify runs, the data in the text field is good.


    JLabel testni = new JLabel (""+ vrsticeField.getText() + " " + stolpciField.getText()); //<<<<<<<

  13. #13
    hero_donkey is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Verifying of Month
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 2
    Last Post: 11-02-2010, 05:34 PM
  2. Replies: 7
    Last Post: 08-29-2010, 11:33 AM
  3. input masking 24hour time format (hh:nn) to jTextField
    By newbiejava in forum New To Java
    Replies: 2
    Last Post: 07-24-2010, 02:12 PM
  4. Replies: 0
    Last Post: 01-27-2009, 08:14 PM
  5. Remembering
    By whosadork in forum New To Java
    Replies: 7
    Last Post: 09-29-2008, 03:11 PM

Posting Permissions

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