Reputation: 1121
I'm a bit desperate over here. I Have this jFrame and I need to close after I press Escape. This is easily done by using keyTyped Event. In the keyTyped event I tried to use System.exit which closes the window, but the process is still running in the task manager (and in the netbeans, even If I run from the jar file, it is still running in the task manager).
I have tried dispose, setVisible(false) along with other things but nothing seems to work.
EDIT:
Code
public Sketch(int width, int height)
{
sketch = new JFrame();
area = new JLabel();
sketch.setUndecorated(true);
sketch.setMinimumSize(new Dimension(width, height));
sketch.setSize(width, height);
area.setBounds(0, 0, width, height);
sketch.getContentPane().setLayout(null);
sketch.getContentPane().add(area);
sketch.pack();
sketch.setLocationRelativeTo(null);
sketch.setVisible(true);
sketch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageBGR = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
imageGRAY = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
keyEvents();
setup();
Thread t = new Thread()
{
@Override
public void run()
{
running=true;
while(running)
draw();
}
};
t.start();
}
private void keyEvents()
{
sketch.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e)
{
if(e.getKeyChar()==KeyEvent.VK_ESCAPE)
{
running=false;
System.exit(0);
}
}
});
}
NOTE: The setup function is a blank function that I override when extending this class.
EDIT2: SOLVED
I found out what I was doing wrong. It appears that in the class that I was extended this one, I was using a webcam. When I call the System.exit function the webcam led is turned off, so I thought I didn't need to properly release it, but it was in fact needed.
Upvotes: 2
Views: 1797
Reputation: 285403
Your running
variable needs to be declared volatile
, and you need to set it to false at some point before closing.
Having said that, this code looks dangerous and bad:
Thread t = new Thread()
{
@Override
public void run()
{
running=true;
while(running)
draw();
}
};
t.start();
You've no Thread.sleep(...)
within there, so it risks hogging the CPU, and as it looks like you're trying to modify Swing state off of the Swing event thread, you're in danger of intermittent thread failure. This suggests to me that you want to use a Swing Timer instead.
Note that for more complete help, consider posting a minimal code example that demonstrates your problem, an SSCCE. This will allow us to run your code and modify it and perhaps even correct it. Please read the link before replying as it supplies many important details on the SSCCE requirements.
Upvotes: 3
Reputation: 2871
The default behaviour when closing your frame is not to exit the program, but just to close the window. In order to exit the program you have to do:
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
You can also do:
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Upvotes: 5