addKeylistener() not working - java

For school I have to make a small game which is based on Breakout.
I got my JFrame which does this:
game.setFocusable(true);
setContentpane(game);
in my game I am adding a inputhandler which extends Keylistener and implements JPanel.
setFocusable(true);
Inputhandler input = new Inputhandler();
addKeylistener(input);
It just doesn't seem to work, I've been writing a lot of tests but I can't see to get the input handle capture any keyPressed.
When I change my JFrame to:
add(game);
it works like it is meant to work but the problem I encounter when doing this way is painting my panels the correct way. I'm kinda stuck on this issue so please someone help me out.
Point I've reached now:
public Game(){
setFocusable(true);
requestFocus();
requestFocusInWindow();
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getActionMap().put("pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Space is pressed");
}
});
this.inputHandler = new InputHandler();
addKeyListener(this.inputHandler);
setPreferredSize(new Dimension(500,500));
}

If I had a dollar for every time this question were asked, I'd retire rich. As per previous similar questions...
Yes you would need to make the JPanel focusable for its KeyListener to work
And you'd also have to give it the focus, since being focusable is not enough. Usually this is achieved by calling requestFocusInWindow() on the listened to JPanel.
And nothing else can have the focus or steal the focus if the KeyListener is to continue functioning.
Which is one of several reasons why most of us recommend against use of KeyListeners for Swing applications
And usually in favor of using Key Bindings.
Edit
I've used your code and it works, both the key bindings and the KeyListener:
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JPanel {
private InputHandler inputHandler;
public Game() {
setFocusable(true);
requestFocus();
requestFocusInWindow();
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
getActionMap().put("pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Space is pressed");
}
});
this.inputHandler = new InputHandler();
addKeyListener(this.inputHandler);
setPreferredSize(new Dimension(500, 500));
}
class InputHandler extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
System.out.println("key pressed");
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println("key released");
}
}
private static void createAndShowGui() {
Game mainPanel = new Game();
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Related

Key bindings does not run

I work java program language in netbeans. I want to move a jlabel box by means of use keybindings. Box can not move when ı keystroked. For example I keystroke w,a,s,d but box cannot move. When I press these keys, it should go up, down, right and left, but there is no movement in the program. The box stays where it is. What should do ı?
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class BoxGame {
JFrame frame;
JLabel label;
Action upAction;
Action downAction;
Action leftAction;
Action rightAction;
BoxGame(){
frame = new JFrame("Keybinding");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,450);
frame.setLayout(null);
label = new JLabel();
label.setBackground(Color.red);
label.setBounds(100,100,100,100);
label.setOpaque(true);
upAction = new UpAction();
downAction = new DownAction();
leftAction = new LeftAction();
rightAction = new RightAction();
label.getInputMap().put(KeyStroke.getKeyStroke("w"), "upAction");
label.getActionMap().put("upAction",upAction);
label.getInputMap().put(KeyStroke.getKeyStroke("s"), "downAction");
label.getActionMap().put("downAction", downAction);
label.getInputMap().put(KeyStroke.getKeyStroke("a"), "leftAction");
label.getActionMap().put("leftAction", leftAction);
label.getInputMap().put(KeyStroke.getKeyStroke("d"), "rightAction");
label.getActionMap().put("rightAction", rightAction);
frame.add(label);
frame.setVisible(true);
}
public class UpAction extends AbstractAction{
#Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX(), label.getY()-10);
}
}
public class DownAction extends AbstractAction{
#Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX(), label.getY()+10);
}
}
public class LeftAction extends AbstractAction{
#Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX()-10, label.getY());
}
}
public class RightAction extends AbstractAction{
#Override
public void actionPerformed(ActionEvent e) {
label.setLocation(label.getX()+10, label.getY());
}
}
}
Start by making use of a more "gloabl" InputMap, the one you are using requires the component to be focusable AND have focus.
Avoid using KeyEvent.getKeyStroke(String), it does some weird things and isn't going to help you, instead, supply the actual KeyEvent, for example
InputMap inputMap = label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "upAction");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "downAction");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "leftAction");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "rightAction");
I'd also recommend making the time to learn about custom painting, as components don't tend to work well like this.
See Painting in AWT and Swing and Performing Custom Painting for more details

How to keep a component in the background window stay in focus?

In my program, I have this MAIN window and a HELP window. The HELP window (when opened) is to always stay on top whether it's in focus or not. The issue however is, when I try to requestFocusInWindow() for a component in the MAIN window through an action listener that gets fired from the HELP window, it just won't let me do it.
What is the proper way of accomplishing this?
TY :)
Edit:
As requested, here's a short example of what I'm trying to accomplish. Essentially I need the button inside the HELP window to trigger focus to the TextField inside the Main window.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void initGUI() {
mainFrame = new JFrame("Main");
helpFrame = new JFrame("Help");
mainFrame.setPreferredSize(new Dimension(500, 200));
helpFrame.setPreferredSize(new Dimension(500, 200));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
helpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new FlowLayout());
helpFrame.setLayout(new FlowLayout());
mainTextView = new JTextField("", 20);
mainButton = new JButton("Open Help");
helpButton = new JButton("Request Focus");
mainButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(mainButton)) {
helpFrame.pack();
helpFrame.setVisible(true);
}
}
});
helpButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(helpButton))
System.out.println("Focus requested:" + mainTextView.requestFocusInWindow());
}
});
helpFrame.add(helpButton);
mainFrame.add(mainTextView);
mainFrame.add(mainButton);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGUI();
}
static JFrame mainFrame, helpFrame;
static JTextField mainTextView;
static JButton mainButton, helpButton;
}
So turns out the fix was rather trivial. If requestFocus() is used instead of requestFocusInWindow(), it seems to work just fine.
Kinda feel stupid for how much time I spent on this :P
helpButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(helpButton))
mainTextView.requestFocus();
}
});

JLabel that changes text from one thing to another

I'm working on this program and I ran into another issue. I have a Jframe with a JLabel that I wish for it to change text from one thing to another. However, when I try to do that it doesnt show me the text changing, rather the last text I set it to.
How do I get my JLabel to cycle through text SLOWLY?
I'm trying a wait method to make the program go slowly so I can see if I can make it cycle through, but that doesnt seem to be working.
it would be helpful if someone could edit my code or make their own example of how to do this, THANKS!
public class CreditGraphics {
public String cardNum;
public JFrame frame;
public JPanel panel;
public JLabel label;
public JTextField text;
public CreditGraphics() {
synchronized(this){
try {
frame = new JFrame("HI");
panel = new JPanel();
label = new JLabel();
text = new JTextField(16);
panel.add(label);
panel.add(text);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(500, 500));
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
wait(4000);
label.setText("Hi");
wait(4000);
frame.revalidate();
frame.repaint();
label.setText("Hello");
frame.revalidate();
frame.repaint();
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardNum = text.getText();
}
});
}
catch(InterruptedException e) {
e.printStackTrace();
}}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CreditGraphics();
}
});
}
public void checkCard(){
}
}
As suggested by #trashgod use Swing Timer that is more suitable for swing application to perform a task once, after a delay or to perform a task repeatedly.
sample code:
private Timer timer;
...
label.setText("Hi");
// delay of 4 seconds
timer=new Timer(4000,new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
label.setText("Hello");
// timer.stop(); // stop the timer if repeated mode is on
}
});
timer.setRepeats(false); // you can turn-on it if needed
timer.start();
Note:
There is no need to call frame.repaint() and frame.revalidate() in this case.
Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.
sample code:
JPanel panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(..., ...);
}
};
read more...
Do not use Thread.sleep() or wait() as it will freeze your Swing application.
Instead you should use a javax.swing.Timer
See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.

JPanel doesn't want to focus so KeyAdapter doesn't work

Background
I had a simple game. I had one JPanel class and there were every thing (menu, game, game end).
Then I decided, that I should make my game better and I made two panels(one for menu, and second for game lvls).
Every thing were good, but my KeyAdapter class doesn't work at my JPanel. I don't know why it doesn't want to focus.
There is what I have:
Main class which extends JFrame and here I add my panels (and KeyListener to first panel)
public class JavaGame2 extends JFrame {
public JavaGame2(){
gamePanel = new GamePanel();
menuPanel = new MenuPanel();
setContentPane(menuPanel);
menuPanel.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
menuPanel.changeCursor();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
menuPanel.changeCursor();
}
if (menuPanel.getCursorPos()==1){
if ((e.getKeyCode() == KeyEvent.VK_ENTER)) {
setContentPane(gamePanel);
//add(gamePanel);
}
}
else{
if ((e.getKeyCode() == KeyEvent.VK_ENTER)) {
System.exit(0);
}
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setTitle("JavaGame2");
setResizable(false);
}
public static void main(String[] args) {
JFrame jgame = new JavaGame2();
jgame.setVisible(true);
}
}
MenuPanel class extends JPanel
public class MenuPanel extends JPanel implements ActionListener{
public MenuPanel(){
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
setSize(800,600);
}
public void actionPerformed(ActionEvent e) {
}
}
And here GamePanel class
public class GamePanel extends JPanel implements ActionListener{
public GamePanel (){
addKeyListener(new GameAdapter());
setFocusable(true);
requestFocus();
setBackground(Color.BLACK);
setDoubleBuffered(true);
setSize(800,600);
}
private class GameAdapter extends KeyAdapter{
public void keyReleased(KeyEvent e) {
ship.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
ship.keyPressed(e);
}
}
}
It doesn't work. GamePanel don't want to focus, I tried to do every thing that I read.
I think u will say that JPanel is not focusable component. But when there was one panel it somehow worked.
How can I fix this focus problem?
Maybe u will say that u prefer don't use KeyAdapter, but I think it looks pretty nice in my code.
setFocusable()? or requestFocus()? requestFocusInWindow()? How should I use them? Maybe I have mistake before and this is not my first problem?
Thanks in advance.
And Thanks for editing.
Short answer, use the key bindings API, it allows you to control the level of focus that a component requires before key events are triggered.
Longer answer, the active panel needs to have keyboard focus. You can use requestFocusInWindow, but there is no guarantee that the component will actually receive focus. When to call this is a tricky thing. You could try overriding addNotify of the panels and calling to there, just make sure you call super.addNotify first, weird things happen when you don't
You will also need to consider what will happen if the component loses focus
As a side note:
setDoubleBuffered(true); is irrelevant, as Swing components are double buffered by default. Generally you might disable this if you wanted to print the component. No harm in calling it though
Calling setSize on your components is irrelevant, as you components will be under the control of a layout manager, which will determine the size of the component itself. You'd be better off overriding getPreferredSize and returning an appropriate size for the layout manager
Calling setSize on JFrame is also a bad idea. Frames have borders, this means that your viewable area will be the frame size - the frames border insets, which will be less the 800x600 you've specified. Better to utilise the previous comment and call pack on the JFrame, which will pack the frame around the content so that it meets the contents requirements...
Personally, I would also localise the KeyListener to the actually component itself, this allows the component to act as it's own controller making it more portable...IMHO
Updated with controller idea...
A "really" simplified concept would be to have some kind of "controller" that the menu and game panel could communicate through, for example...
public interface GameController {
public void showMenu();
public void showGame();
}
You would then pass a reference of this interface to the MenuPanel...
public class MenuPanel extends JPanel implements ActionListener{
public MenuPanel(GameController controller){
//...
}
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
And the GamePanel...
public class GamePanel extends JPanel implements ActionListener{
public GamePanel (GameController controller){
//...
}
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
You would, obviously, need to construct a implementation of the controller...
public class CardLayoutGameController implements GameController {
public static final String GAME_PANEL = "GamePanel";
public static final String MENU_PANEL = "MenuPanel";
private Container container;
private CardLayout cardLayout;
public CardLayoutGameController(Container parent, CardLayout layout) {
container = parent;
cardLayout = layout;
}
public void showMenu() {
cardLayout.show(container, MENU_PANEL);
}
public void showGame() {
cardLayout.show(container, GAME_PANEL);
}
}
You would then construct your UI around this controller, for example...
public class JavaGame2 extends JFrame {
public JavaGame2(){
CardLayout layout = new CardLayout();
setLayout(layout);
GameController controller = new CardLayoutGameController(getContentPane(), layout);
gamePanel = new GamePanel(controller);
menuPanel = new MenuPanel(controller);
add(gamePanel, CardLayoutGameController.GAME_PANEL);
add(menuPanel, CardLayoutGameController.MENU_PANEL);
controller.showMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setTitle("JavaGame2");
setResizable(false);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame jgame = new JavaGame2();
jgame.setVisible(true);
}
});
}
}
Is this just an "example" to help spark the idea, haven't tested this, just hacked out here so it might blow up :P

Closing a JDialog by hitting the "enter" on keyboard

I want to close my JDialog by hitting the "enter" key on my keyboard. how can I do that? thank you!
NOTE:
I want to do this, without any button involved.
THank you!
One way:
You could give it a close JButton
whose ActionListener has code that closes the dialog,
And make that button the default button for the dialog's rootpane.
e.g.,
myDialog.getRootPane().setDefaultButton(exitButton);
Option two:
Use Key Bindings to bind the enter key to exit code in an AbstractAction.
e.g.,
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DemoDialog {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.add(Box.createRigidArea(new Dimension(400, 300)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final JDialog dialog = new JDialog(frame, "Dialog", true);
// set binding
int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
String enter = "enter";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
actionMap.put(enter, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
}
I would like to say first that 'Hovercraft Full Of Eels' solution is more elegant than this one and more closely in the spirit of the JDialog and Swing API. However, to offer an alternative here is a basic example of using a KeyListener on your JDialog that will do as you need without adding a button;
public class Test {
public static void main(String[] args) {
JDialog jd = new JDialog();
// Add and define the KeyListener here!
jd.addKeyListener(new KeyListener(){
#Override
public void keyTyped(KeyEvent e) {
// Nothing
}
#Override
public void keyPressed(KeyEvent e) {
// Nothing
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
JDialog d = (JDialog)e.getSource();
d.dispose();
}
}
});
// End key listener code
jd.setVisible(true);
}
}
The important/relevant code is between the two main comments. This is a compilable example, so you can copy paste this into a new file and run it to view the effects.

Categories