When I remove all of the MouseListener components and run the applet I have to click on it to give it focus, then the KeyListener works perfectly. To solve this I added an "opening page" where you have to click on start before the game runs. This would give the applet focus and not start until the player was ready at the same time. I add in the MouseListener and it works great, but now the KeyListener does not register at all. I can not find out why, but I imagine it has something to do with focus again. Anyone know what should be done here?
Code can be found here: http://pastebin.com/LDxtk878
Thanks!
Without examining all 322 lines of your code, you might look at the article How to Write a Key Listener with attention to the section following "Note: To fire keyboard events, a component must have the keyboard focus."
Also, consider extending JApplet, as an alternative.
Related
I added mouseListener to a button and when mouse moved to it it performs some action. I added an actionListener to same button and it removes the button. My question is: when I try to remove the button I get runtime error. How can I remove the button?
Regarding:
is it posibble to remove a mouseListener in Java
Yes, simply call myComponent.removeMouseListener(myMouseListener); and it's gone. You of course would need a reference to the said MouseListener.
Also regarding:
I added mouseListener to a button
You generally don't want to do this, and I'd be interested to know how you're using this. JButtons should get ActionListeners added to them and not MouseListeners.
and when mouse moved to it it performs some action.
If it's to listen for hovering, you might be better adding a ChangeListener to the JButton's model and query for isRollover(). -- OR -- perhaps what you really need is to set the JButton's tool tip text via the setToolTipText(String text) method.
I added an actionListener to same button and it removes the button. My question is: when I try to remove the button I get runtime error. How can i remove the button?
Here we're stuck since we don't have access to pertinent code or your error message. If you need more help, please improve the question by providing pertinent code, preferably as a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem. Also post the complete error message as well as an indication of what lines cause the error (something the error message will tell you).
I have a fairly simple Java Application I created with JFrames. There is more than one JFrame in my program. I have a menu launching at the beginning of the program. After going through the menu, I have the menu JFrame disposed.
The main JFrame has a menu button that should launch the exact same menu at a later time. However, when you launch the menu from inside of the ActionListener (when you press the menu button), the JFrame doesn't launch properly. None of the components show up and colors are off.
However, when the menu is launched from outside of the ActionListener, the JFrame shows up perfectly. I have tried multiple methods to fix this, but none of them have worked.
My full program code is available by clicking here.
The main class is "LetsMultiply5.java". This class also sets up the ActionListener.
The JFrame causing the problem is "MenuWindow.java".
"LetsMultiply5.java" calls the "Booter.java" class, which then calls the "MenuWindow.java".
"MainWindow.java" is the JFrame that has the "Menu" button.
For proof, "SpeedModer.java" calls the menu window after it has been disposed, and works.
================================EDIT================================
Also, I'd like to let you know that I realize my code is a little bit messy. I am not sure how else to write the code for this program.
I am forced to use Thread.sleep(x); because the Swing timers aren't what I am looking for. The Swing timers activate an ActionListener when the timer goes off. I need a system that will wait a second before continuing on with the code.
I realize that the while (repeater==0) loop with ActionListeners inside of it seems crazy, but that was the only way I could get it to work. If I put a single ActionListener and just had the while loop do no code inside of it, nothing happens when I press the button.
I would, as MadProgrammer mentioned:
Advice: Scrap your current approach and start again.
However, the way that I have my program currently coded is the only way that I know how to get what I need to do done. I read the tutorials, but still don't know how to improve the code in the way that you told me.
I thank everyone for trying to tell me to improve my bad "Java grammar", but as far as I am concerned, I am not going to continue this program for the next 20 years and make my millions off of it.
I have looked at the Swing timers before and I understand the whole new Timer(speed, this); concept, but I don't understand how this would make my code any better.
If anyone would like to show me how to fix my ActionListeners or Thread.sleep(x); lines, please tell me. Thank you.
You're blocking the Event Dispatching Thread with Thread.sleep(3000); - Don't do this, it will prevent the UI from been painted
See Concurrency in Swing for more details about the problem and How to use Swing Timers for a possible solution
You may also want to consider having a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using CardLayout or JTabbedPane
If you need to block the user temporarily (to display a error message or gather important details), consider using a modal JDialog. See How to Make Dialogs for more details
I'm trying to program a simple game using Java 1.4.2
It uses two main screens, both of which work using JPanel. One of them only has a MouseListener and works flawlessly; using the first one, you can open up the second one. The second screen uses a MouseListener and a KeyListener. The mousePressed method works fine. However, the keyPressed method doesn't work at first; to get it to work, I have to minimize the window, then reselect it again before it does work.
In the first screen, I was advised to put a toFront() command when opening up the second screen; i.e. secondFrame.toFront() However, it gives me an error, saying No method "toFront()" was found in type "secondFrame"- even though secondFrame extends JPanel.
I've heard some information on something called "Key Bindings"; I'm unfamiliar with the topic, but I'd rather solve this problem at its core, without just resorting to a work around. If possible, I'd like to solve this while still using KeyListener.
Snippet of code:
public TowerDefenceBoard{
(generic stuff)
setFocusable (true);
addKeyListener (this);
requestFocusInWindow ();
}
Does anyone know why this happens? Does anyone have any advice?
Thanks in advance :)
Try calling requestFocsInWindow() on the panel with the KeyListener
I need to keep keyboard input focus on a single component inside a JPanel. It's for an application with a on-screen-keyboard.
Not sure I really understand the question. But you can try something like:
otherComponents.setFocusable( false );
You also might need to use a custom FocusTraversalPolicy.
If you need more help then post a SSCCE that demonstrates the problem.
You better put the focus back to the component (component.grabFocus()) after pressing a button on the on-screen keyboard.
Or you could set focus listener (component.addFocusListener(FocusListener l)) and never let go of the focus by calling grabFocus() in the focusLost() method of FocusListener.
This will give you focus on your singleComponent when opening your Frame, without having to change the focus policy of enything else: singleComponent.requestFocusInWindow(); Since the focus will not freeze, you will need to setFocusable(false) for the other components like camicr suggests.
Just a guess: take a look at the InputVerifier.
I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should.
This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I can't handle press events etc, but the mouseListener works fine and I can still handle mouse move event.
Here is a screen shot how it works without the ToolBarPane
http://img684.imageshack.us/img684/3232/sampleku.png
note that you can use the mouse to put images on the map, you can also select images using the mouse just like a laser tool, and by pressing number key you can switch between different images, this works fine until I add the ToolBarPane which shows like this:
img291.imageshack.us/img291/8020/failve.png
(please add http before that, I can only post one hyperlink)
(I can't post images here because I'm a new user)
With the ToolBarPane on I was no longer able to handle the key event.
I guess it might by that the focus as been transferred to that panel somehow, but not sure at all.
Does and body know this and can help me out?
Thanks very much
I suggest you use the InputMap and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT or something similar. Excerpt from How to Use Key Bindings:
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
The component contains (or is) the component that has the focus. This input map is commonly used for a composite component
That has worked very robustly for me. Have a look at my other post for more information and actual code examples:
Keyboard input for a game in Java
or this tutorial:
Swing: Understanding Input/Action Maps
You should NOT be using a KeyListener.
Swing was designed to use Key Bindings which is far more flexible. Check out my quick summary of Key Bindings which also includes a link to the Swing tutorial which conains far more detail.
(I can't post images here because I'm a new user)
An image doesn't help much anyway. If you need more help post your SSCCE which shows the problem (after trying the above suggestion).