Re: Trouble with JTextfield
Quote:
Originally Posted by
loui345
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.
Quote:
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!
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.
Code java:
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?
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?
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.
Re: Trouble with JTextfield
Quote:
Originally Posted by
loui345
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.
Re: Trouble with JTextfield
Can you please elaborate on what "trim()" means. Thank you so much.
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.
Re: Trouble with JTextfield
Wonderful! Glad it helped!