Quick question, I have 3 JPanels, one has a keylistener and it draw my game graphics, and you can alter it by moving your character around, etc. and I have a second JPanel which is just a JTextArea, and it just displays text, and finally I have another JPanel which has a JTextArea and a JButton, the text area takes inputs and you may hit enter key or click the button to send the text which will appear in the other text box. But my question is, how can I request focus in each panel? How do i transfer the focus with lets say a mouse click?
I know that if I dont constantly call requestFocus(true); for the first JPanel with displays all the graphics it wont work for some reason. I believe its because its always painting and maybe it looses focus on every go around. But how to I in all get the focus to transfer with a mouse click, etc?
One possible solution is to not use a KeyListener. Instead use Key Bindings which are much more forgiving with regard to focus. Have a look at the Key Bindings tutorials which Google can help you find in a jiffy.
For more specific help, consider creating and posting an sscce.
Related
I'm about to write a program using Java and i want it to have the next behavior:
Start with a small screen, just one button (i'm going for the JMenuBar) for the user to select a image file (a country or state map)
Once selected the image file, i'll need to resize the frame to the size of the selected image, and put the image as background.
when the user clicks somewhere inside the frame (click on a state or city) the program will have to create a visual object there, a circle, square or any form in that coordinates.
will need also a listener in those objects to know when they are clicked.
Summary: User has to select an image and trace a graph on it.
I am not asking for the code to do this. I would like to have some ideas about which components use to achieve this since i have been reading and there are plenty of ways to set the background image and stuff. But, considering the requirements, can you recommend me which components to use? I am a bit short of time since i've been given only about a week to code this, otherwise i would try all the alternatives by myself.
Some answer like:
"use a label to set the background and then resize the frame by this way: (some stuff) and then you can create a class extending from JLabel to create the circles with the listeners...." that would be enough help
I hope I was clear, any idea is welcome
Many thanks!
If you're going to stick with Swing I would use a JFileChooser to select the image. Once you got the image you can easily resize the JFrame by using the frame.setSize(image.getWidth(), image.getHeight());
To listen for mouse clicks inside your JFrame you need to use a MouseListener, make sure to add it to the frame, I always forget doing that.
Not sure whether you've succeeded drawing images/shapes at all. If not, you need to use a JPanel, check this topic if you need extra help.
If you are going to use a "JFrame " then you should definitely use Swing JFrames JPanels, and JLabels (as well as any other JComponents you need.) to accomplish this. Use only one JFrame. Use JPanel as the content pane/background for your JFrame and add everything else to it. But I would also suggest learning and using JavaFX because its the newest and I think it would be the easiest to use to do something like this. But if you only have a week and you know some swing use what you know. If you need more information post some code. Or ask a more direct question.
Okay so long story short, here is what is happening and what I am trying to do.
I have a class called GameGUI, this is a JFrame.
This JFrame is populated with tiles(JLabels with ImageIcons)
I use my arrow keys to move my guy around the JFrame (just updates the JLabel Images)
I made a settings option, this is a new JFrame that I setVisbile(true), make changes, then setVisible(false)
After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
I have tried the following, oh which none work: (all guesses based off Googling my problem)
GameGUI gg = new GameGUI();
gg.setFocusable(true);
gg.addKeyListener(null);
gg.requestFocusInWindow();
I cannot seem to find a way to get my KeyListener to move back to the GameGUI after I open (make visible) this settings menu then close it (make invisible). I do have radio buttons within the settings menu which is why I believe it gains focus, due to a physical mouse click.
Any help on getting focus back onto GameGUI would be much appreciated!
Thanks!
5.After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
See Motion Using the Keyboard, which will explain your probable problem and give the solution using Key Bindings.
I have two JPanels. In the first one I have 3 JButtons and the second one is to draw images based on events read from the keyboard. If I set the JButtons with setEnabled(false); I can use the keyboard events as I expect (If I press up arrow the image moves up), but when the buttons are enabled nothing happens with the image. Even, if I press the space bar it behaves like if I clicked the button.
The problem is not with the JButtons, but is likely because your using a KeyListener. Don't use KeyListeners with Swing GUI's if you can avoid it and instead use Key Bindings. KeyListeners only work if the component being listened to has the focus, and when you have JButtons present, they will take the focus and prevent your KeyListener from working. Key Bindings, if done right, can avoid this problem.
For example, please see my code example here.
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true); and button.addKeyListener(this); to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
button.setFocusable(true); and button.addKeyListener(this); to all my buttons
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric AWT Applet, use JApplet, may be sufficient would be plain JFrame
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.
I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src