Welsar55
Welsar55

Reputation: 39

How to have it so a program stays open while waiting for a event

I want it to stay open and wait for the event to happen but as soon as it opens it closes how do i fix this?

    static void hi()
    {
        System.out.println("g");
    }

public static void main(String[] args) { } @Override public void KeyPressed(KeyEvent e) { hi(); } @Override public void KeyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void KeyTyped(KeyEvent e) { // TODO Auto-generated method stub } <code>

Edit: wait even when i do have a frame when i press a key it dos not run hi()

Upvotes: 0

Views: 82

Answers (2)

Alexey Ivanov
Alexey Ivanov

Reputation: 11848

You do not create any window to get an event. Your main function is empty, so your program does nothing: its process closes after it starts.

Look at Creating a GUI with Swing tutorial. You'll find the simplest Hello World GUI application in Compiling and Running Swing Programs section, see HelloWorldSwing.java.


It's not enough to create a frame: you have to register KeyListener on frame or another component. Here's the complete example:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class PressKey implements KeyListener, Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new PressKey());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Press a key");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.addKeyListener(this);

        frame.setSize(300, 150);
        frame.setVisible(true);
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("Key pressed: " + e.getKeyChar());
    }
}

How does it work? The function main creates an instance of PressKey class and passes it to SwingUtilities.invokeLater utility method. This is required because Swing is not thread-safe and all modifications to GUI components must be performed on the Event Dispatch Thread (EDT). So invokeLater schedules a job onto EDT; as the result PressKey.run() will be run on EDT: it's where we create the frame and register KeyListener attached to the frame, then we show the frame. When the frame is shown on the screen, press any key: you will see the corresponding character printed in the console. If you press a functional key, or another key that does not generate an input character, you won't see anything printed because in this case KeyEvent.KEY_TYPED is not generated.

For more info, see How to Write a Key Listener.

Upvotes: 1

Matt
Matt

Reputation: 3353

I am assuming you have copied this code out of a tutorial? There are a couple of things missing. As Alexey mentioned, the 'main' method is the entry point for your program, and if you have no code in your main method then your program does nothing. You should be doing something like creating a new thread, frame, window, etc whatever it is that you are wanting your key listeners to attach to.

In the broader context of creating a thread that runs forever, you could do something like

while(true) {
    Thread.sleep(100);
}

although you would probably want to change 'true' to 'myflag' which can be set from elsewhere in your code to terminate the thread.

Upvotes: 0

Related Questions