Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Trouble with JTextfield

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Trouble with JTextfield

    Simple question I have is can I have JTextfield store multiple data? For example, say if there is code and it retrieves data from the textfield and then writes it to an excel sheet. I have this working perfectly, but the user has to exit the program and reenter it to enter new data into the excel sheet. What I would like is for the user not needing to exit the program and continue to write to excel. Is this possible? If so, can you please write a bit of code and show me. Last part of my project for my business and I am stuck. Since it's sort of a general idea is it beneficial for me to post my code?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with JTextfield

    Quote Originally Posted by loui345 View Post
    Simple question I have is can I have JTextfield store multiple data? For example, say if there is code and it retrieves data from the textfield and then writes it to an excel sheet. I have this working perfectly, but the user has to exit the program and reenter it to enter new data into the excel sheet. What I would like is for the user not needing to exit the program and continue to write to excel. Is this possible?
    Certainly it's possible.

    If so, can you please write a bit of code and show me. Last part of my project for my business and I am stuck. Since it's sort of a general idea is it beneficial for me to post my code?
    The code will be your responsibility I'm afraid. Most here, myself included aren't familiar with the specifics of your problem or even interacting with Excel, but I can tell you with out doubt that what you desire can be done. So don't give up!

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    loui345 (December 1st, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Trouble with JTextfield

    See, I got it to write into excel perfectly. It's just that the user has to exit the program for it work.
      public void actionPerformed(ActionEvent ae) {
                        CustomerName = jfield.getText();
     
                        jfield.setText(" ");
                    }
                });
     
                jfield2.addActionListener(new ActionListener() {
     
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                       OrginalPriceBought = jfield2.getText();
     
                        jfield2.setText(" ");
     
                    }
                });
                jfield3.addActionListener(new ActionListener() {
     
                    public void actionPerformed(ActionEvent ae) {
                       AmountSoldFor = jfield3.getText();
                        jfield3.setText(" ");
     
                    }
                });
                jfield4.addActionListener(new ActionListener() {
     
                    public void actionPerformed(ActionEvent ae) {
                        USShipping = jfield4.getText();
                        jfield4.setText("");
     
                    }
     
     
                });
            jfield5.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae){
                      JapanShipping = jfield5.getText();
                        jfield5.setText("");
                        IOClass IO = new IOClass(CustomerName,OrginalPriceBought, AmountSoldFor ,USShipping,JapanShipping);
     
                }
                 });

    I thought of putting a while loop into the fields, but it still throws an error. I searched the internet and could not find anything useful. Anyone have a suggestion or a tutorial?
    Last edited by curmudgeon; November 30th, 2012 at 11:13 PM. Reason: edited and corrected code tags

  5. The Following User Says Thank You to loui345 For This Useful Post:

    vigneshwaran (December 1st, 2012)

  6. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with JTextfield

    How are you adding the data to Excel? Using Apache POI? If so, where's the code where you update data into Excel? Also why not just extract all JTextField data at once when a JButton is pressed?

  7. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Trouble with JTextfield

    Yes, I am using Apache POI; see the user enters the data one at a time into the corresponding JTextField box, and when the jfield5 is clicked it calls my Excel class and all the data is then set to excel a total of 5 fields are then printed to excel. I was thinking of using a while loop,arraylist, but a bit confused on how to get it to work.

    --- Update ---

    If the user does not exit the program and restart it, it throws this exception: "
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 100"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)"

    I believe it still holds the value from the 1st time the user inputs data. I am not sure of course.

  8. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with JTextfield

    Quote Originally Posted by loui345 View Post
    Yes, I am using Apache POI; see the user enters the data one at a time into the corresponding JTextField box, and when the jfield5 is clicked it calls my Excel class and all the data is then set to excel a total of 5 fields are then printed to excel. I was thinking of using a while loop,arraylist, but a bit confused on how to get it to work.

    --- Update ---

    If the user does not exit the program and restart it, it throws this exception: "
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 100"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)"

    I believe it still holds the value from the 1st time the user inputs data. I am not sure of course.
    It looks like you should trim() some Strings before submitting them. And no, don't use a while loop or an array list as that won't work for this.

  9. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Trouble with JTextfield

    Can you please elaborate on what "trim()" means. Thank you so much.

  10. #8
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Trouble with JTextfield

    I did a few google searches and it worked thank you. I can now do it many times and the program does not need to be restarted.

  11. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with JTextfield

    Wonderful! Glad it helped!

Similar Threads

  1. Having trouble storing variable from JTextField
    By sibs in forum What's Wrong With My Code?
    Replies: 32
    Last Post: December 1st, 2012, 08:39 AM
  2. [SOLVED] Trouble clearing JTextField, getting NullPointerException
    By Lanst83 in forum AWT / Java Swing
    Replies: 11
    Last Post: September 22nd, 2012, 06:37 PM
  3. [SOLVED] JTextfield
    By usherlad in forum Java Theory & Questions
    Replies: 4
    Last Post: August 22nd, 2012, 11:24 AM
  4. JTextField
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 7
    Last Post: June 20th, 2012, 10:54 AM
  5. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM