• Post Reply Bookmark Topic Watch Topic
  • New Topic

problems adding keyListener to JFrame class

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello!

As you can see I'm adding the keyListener in my initComponents() method, but it doesn't work. I wrote System.out.println("whatever"); just below keyPressed, and it should print that when I press a key, but it doen't print anything.

There are many things from my code missing in this post, but this is the part where I call the keyListener.

Can you tell me what's wrong??



Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post code that compiles, executes and shows the problem?
The posted code does not compile without errors.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole project?

It's made of 4 classes
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not the whole project. Make a small, simple complete program that compiles, executes and shows the problem.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles,but it doesn't show the window with the maze (which is the program that I'm doing) now


 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesn't show the window with the maze

Does the code you just posted show the problem?

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean with " the problem"?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I press a key, but it doen't print anything.

Why did you start this thread? Do you have a problem with your code?

I'm looking for a small, simple program that shows that problem.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started this thread because I'm doing a project that consists in creating a maze game in Java.
I created the walls of the main, the piece, EVERYTHING. And it works. However I can move the piece inside the maze. I added the KeyListener but it doesn't do anything when I press a key.

To check what or where the problem was a put a System.out.println("whatever") inside the keyPressed method, so when I press any key "whatever" is printed, so I know if the problem was in detecting what key I was pressing. However, it doesn't print anything, which I guess it means the program doesn't even enter the KeyListener method. I don't know why. I posted all my code above.

I don't know how to take four classes and create a simple program that works, as you say. I put all the classes together in one class, but the window with the maze doesn't appear.
I can post here the four separate classes, so you can check what's wrong. I really need help, because it's my final project for university.

Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of trying to work with a very large program to find out how to create a key listener, I suggest that you work with a small, special program for testing what is needed to make a key listener.
When you get it working, copy the logic to the large program.

When I compile and execute the code you posted, I get this message: vengo de buenonull
and the program ends.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to make a KeyListener, but it doesn't work in this program.
I know that it just gives that message, that's because I don't know how to put the classes together and still make it work.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as an example, this one works.


I can't figure out why in the maze it doesn't. It doesn't even enter the keyListener!! WHY?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't even enter the keyListener!! WHY?

The most common reason is because the component with the keylistener does not have the focus.


When I compile and execute the big code you posted, I get this message: vengo de buenonull
and the program ends.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second one?? It works for me.

I can't put the maze program in just a single class.

If it doesn't bother you too much, could you take a look at the whole project?

MAIN CLASS: Laberinto



CLASS Casilla:



CLASS Ficha (you will need the image I attach, because this is the piece I want to move)



CLASS Mapa:



Note: the code for the moment inside every CASE is not right, I know. But if I could only know that a key is pressed, it would mean that it works.

Thank you






ficha.png
[Thumbnail for ficha.png]
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile and execute the big code you posted, I get this message: vengo de buenonull
and the program ends.

What is needed to make the code work on my PC?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check my last post above yours if possible PLEASE. You will need an image to create the Ficha (the piece that moves inside the maze)

Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the image in the folder with the class files.
When I execute the program I get this message:
vengo de buenonull
and the program ends,
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you create a new project with the four separated classes?
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the program print out that message when I compile and execute it?

I don't have an IDE that uses projects. I have a fancy editor that wraps the javac and java commands.

I see that there are many catch blocks without calls to the printStackTrace() method. That means the program will NOT report any exceptions that are thrown.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints that message because somewhere in the code I told the program to do so, just to check if one part worked.
I use NetBeans and as I said previously I don't know how to take my four classes and do a program that does the same but just in one single class so you can run it.
If you tell me how to do it I'll do it
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It prints that message because somewhere in the code I told the program to do so, just to check if one part worked

You need to Look at the code and find out why it prints that message and then tell me what I need to do so that the program will execute and show the problem you are having.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which code of the ones I sent are you executing?

Anyway the problem is not that it prints the message, it's the keyListener that doesn't work
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the code that was posted at 7:47:08 PM

If you don't tell me what I need to change for the program to execute after printing the message: vengo de buenonull
I can not help you.

You MUST add a call to printStackTrace() to all of the empty catch blocks!!!
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It comes from this part, where I take the file I want to read in order to create the maze.
Maybe it doesn't work because we are not using the same IDE. In my computer, I run the project and it appears a window with the maze.

I will add that and show you what happens
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The IDE is not the problem. Where is the file that the program is trying to read?

You MUST add a call to printStackTrace() to all of the empty catch blocks!!!

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
God, the files!! what a fail. I'm sorry, I didn't think about it, even didn't mention it.



I can't attach them.

I write them here. (it's enough with one)

maze1.txt

15
10
1011101010101010100010101010101010101100
1001110010011010011010011100110110010110
0101010101011001101001100011011000111000
0101010101010101100110101100100110100110
0101001101100101001111000101010110111100
0101110110010000111001010011011010010100
0101001101100001101001101001110001010101
0011101011000011100010100110001101100101
1001101001101101010110011100100111100101
0001110010110000011001010101000110100110
0101001111000111100101100101010110011110
0011110000111010000011100101010100111100
1011011010011110011110010110001111000101
1001101001101001110000111010110000110100
0011101010100110001110101010011010110110
2
10





 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now it executes and displays an image. Now what do I have to do to show the problem?
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see I added a KeyListener, to move the piece using the keyboard, but it doesn't detect when I press a key.

It's inside the main class, in the initComponents() method.



Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is s conflict and confusion with the 2 JFrames the code creates.
One is created on line 31 ( Laberinto extends JFrame) and the other on line 35. Get rid of one of those JFrames.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And if I delete extends JFrame nothing works
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may not be that simple. Redesign and rewrite the code so that it only uses ONE JFrame instead of two.

if i delete this one then how do I do the code below??

Replace the variable: VENTANA with the this variable.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did that. THANKS .Now it detects the key.
The problem is that I have other classes inside initComponents() that use VENTANA and "this." doesn't work here:

 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a variable in the outer class that holds a reference to the JFrame instance you want access to and use that. For example:
Put that outside of the inner classes where they can access it.

 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the outer class? Which one? I don't understand where
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand where

The same place VENTANA was defined. Line 35
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this that works fine


but when I write this. inside the methods elegir(), reiniciar() it doesn't work. (cannot find symbol getVisible, etc)

 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code needs to use the new variable: theFrame to access the instance of the JFrame class.

Ok here is a simpler change to make. Using the original code from the post Yesterday 7:47:08 PM, change line 35 as follows:
Don't make any of the other changes, only that one.


this.setVisible(true); //cannot find symbol

That is because the class that the code is in (referenced by this) does not have a setVisible method.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now detecting a key being pressed works fine.
Now it's time to move the piece.

i'll tell you how it goes, and thank you so much for your help and time!

What's the reason it didn't work and why it works now?? I need to know

Thanks
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the reason it didn't work

There were 2 JFrames
One JFrame had a keylistener but was not shown
the other JFrame did not have a keylistener and was being shown.

To see that, take the original code and add the following after the call to initComponents();

You should now see 2 JFrames, one as expected and one very small.
 
Johnny Januan
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I got it.
Thank you.

This is my new KeyListener:



This is supposed to go Casilla by Casilla (squares) and paint the new Ficha. Do I need a repaint() somewhere else in the program??
Replace the keyListener in the old code you were executing by this one and you will see what happens.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!