Results 1 to 8 of 8
Thread: Need help with a gui program
- 02-26-2012, 06:25 AM #1
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
Need help with a gui program
Hi,im new to java gui programming
I want to create a loop to display numbers(for example:1 to 20) inside jTextField.
Here is what I tried:
Thank youJava Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i =1; int x; while (i<=20) { x= (Integer.parseInt(jTextField1.getText())+1); jTextField1.setText(jTextField1.getText()+ x); i++; } }
- 02-26-2012, 06:52 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Re: Need help with a gui program
What happened when you tried that?
-----
Consider using more descriptive variables and methods than jTextField1 and jButton1ActionPerformed(). As the amount of code increases there is a very real payoff from having variables and methods say, by their very text, what they are and do.
- 02-26-2012, 09:53 AM #3
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
-
Re: Need help with a gui program
If you walk through your code in your head, you'll see that the output you see is what you should expect. Do you know that the + operator has different meanings if you're dealing with Strings vs. numbers, and your method above uses it for both situations, numeric addition and String concatenation.
- 02-26-2012, 05:23 PM #5
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
Re: Need help with a gui program
ok,could you please help me to get the expected result?
-
Re: Need help with a gui program
Let's see what you're doing, but I'm going to split some of the code up for clarity (and you might wish to do this yourself in future code):
So what's going on?Java Code:while (i<=20) { // to replace the line: // x= (Integer.parseInt(jTextField1.getText())+1); String textFieldText = jTextField1.getText(); // self explanatory int x = Integer.parseInt(textFieldText); // self explanatory x = x + 1; // A) // to replace the line: // jTextField1.setText(jTextField1.getText()+ x); String newText = jTextField1.getText(); // B) newText = newText + x; // C) jTextField1.setText(newText); //D) i++; // self explanatory }
- If the textfield initially holds 1, at this point x will hold 2 -- and this is what you want, I think.
- You're getting the String that is held in the JTextField which at this point is "1"
- You're doing String concatenation with the textfield's String and x. "1" + 2 = "12"
- You're setting the JTextField with this new String, "12".
So the solution is not to do String concatenation. You should be able to change your code I think to try to improve it. Please give it a go, and see what you can come up with.
Next we may have to talk about Swing Timers, but let's solve this problem first.
- 03-11-2012, 01:30 PM #7
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
Re: Need help with a gui program
I just looked into it today and found out the mistake I made,^Thank you :)
Here is what I did:
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int i =0; int x=1; while (i<=20) { jTextField1.setText(jTextField1.getText()+" " + x); x++; i++; } }
- 03-11-2012, 01:52 PM #8
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks