• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple error in my program

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I've developed the following class for this problem
(Entering and displaying a string) Write a program that receives a string from the keyboard and displays it on a panel. The Enter key signals the end of a string. Whenever a new string is entered, it is displayed on the panel.

The program doesn't work as expected .Although the frame is displayed well, it doesn't listen to any input from the keyboard
even when i press the close button ,the frame doesn't close
how can i make the program works well ?
I'll be grateful if you give me any notes about this program.
thanks in advance............
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an infinite loop in your ActionListener. Get rid of the while loop.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've modified my program and remove the infinite loop but the program still doesn't work
Do you know the reason?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please describe "doesn't work." Post error messages (exactly), describe what should be happening and what isn't happening, etc.
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read my suggestion?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your KeyListener will be called once for every button press. So, as Rob says, there's no need for a loop in it. At the moment pressing anything except '\n' will still cause an infinite loop, as e.getKeyChar() doesn't change within the keyPressed method.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Camick, Matthew Brown, Greg Brannon for your valuable replies
I've modified the KeyboardPanel class as following:

and there is a problem:
the program take the first string and display it on the panel but after the program take the second string it display it beside the first string and i do not want this
So, How can i clear the panel when a new string is coming to be displayed on it?
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me the benefit of this line in the paintComponent() method?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Fed wrote:So, How can i clear the panel when a new string is coming to be displayed on it?

You need to reset name to blank when enter is pressed.

super.paintComponent draws the background of the component. Delete the line - you'll see for yourself what the effect is.
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew Brown,
I've modify the paintComponent() method to reset the name to blank after displaying it

when i delete the line

the program still working normally
I'd be grateful if you give me any other notes to my program
thanks .
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Fed wrote:Thanks Matthew Brown,
I've modify the paintComponent() method to reset the name to blank after displaying it

And now resize or move your application after pressing enter. That causes a repaint as well. Whoops.

when i delete the line

the program still working normally
I'd be grateful if you give me any other notes to my program
thanks .

Even without the moving / resizing, try typing some characters, press enter, type some more characters and press enter again. Then add that line again and try again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic