I'm working on a programm and have a problem with closing it.
I added the following to listeners:
KeyListener kl = new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if(evt.getKeyCode()== KeyEvent.VK_ESCAPE) {
shutdown();
}
}
};
WindowListener wl = new WindowAdapter() {
public void windowClosing(WindowEvent e){
shutdown();
}
};
public void shutdown()
{
frame.dispose();
System.exit(0);
//dispose of a buffer
}
I also set:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
If I run the the programm in eclipse both listeners close the programm and terminate the process. However if I run it with the runnable .jar only the keylistener works correctly. Closing the window normally does close it, but the process isn't terminated. Why is this and how do I fix this?
Related
In my swings windows application after click the run button some operation is executed. If i am try to close window when operation is still in progress, close operation is not working. After complete the execution of process then only close window operation is working. Otherwise it will not responds the close operations.
I have already tried below mentioned codes. But that one is not stopped working
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
or
System.exit(1);
or
setDefaultCloseOperation(EXIT_ON_CLOSE);
or
setDefaultCloseOperation(3);
or
dispose();
}
});
JButton jb = new JButton("Run");
add(jb);
jb.setBounds(10, 30, 100, 30);
setSize(150,150);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
// some operations . For Example here i'm add 1E5 data,s in my collection object. again i will replace data's places of each element.
for(int i=0;i<1E5;i++)
{
ll.add(i);
}
for(int i=0;i<1E5;i++)
{
ll.add(1,i);
}
for(int i=0;i<1E5;i++)
{
ll.add(0,i);
}
System.out.println(ll);
}
});
If I am clicking close button, my window terminate the currently executed process and Close the window.
class MyThread extends Thread {
public void run() {
// some operations . For Example here i'm add 1E5 data,s in my collection object
..
..
}
}
Then in your actionPerformed method on your JButton actionlistener you just start the thread :
public void actionPerformed(ActionEvent e) {
new MyThread().start();
}
For example, I have code like:
public static void main(String[] args) {
JWindow w = new JWindow();
w.setSize(100, 100);
w.setVisible(true);
// some code need to be executed after window is closed, Eg.
System.out.println("some code need to be executed after window is closed");
}
I want code to stop at w.setVisible(true); until the window is closed/setVisible(false), then execute System.out.println("window closed");, how to achieve this?
you could refer to this link
you can override the windowClosing() method
public void windowClosing(WindowEvent e) {
System.out.println("window closed");
}
or windowClosed()
public void windowClosed(WindowEvent e) {
System.out.println("window closed");
}
based on what you are doing, right after the setVisible(true) start the screen capture in your constructor for example, and then the screen capture should tell you that the process is done so you can proceed to your other code execution. there is no need to pause the code execution.
You can use window closing event or window closed event
public void windowClosing(WindowEvent e)
{
System.out.println("window closed");
}
OR
public void windowClosed(WindowEvent e)
{
System.out.println("window closed");
}
i tried like they answered in this question
How can save some Objects, directly after the User has closed the Applications JFrame, but before the Program exits?
but the frame won't close
i wrote the following but it's the same thing, the frame wont close .
#Override
public void windowClosed(WindowEvent e) {
dispose();
setVisible(false);
System.exit(0);
}
#Override
public void windowClosing(WindowEvent e) {
dispose();
setVisible(false);
System.exit(0);
}
you can add shutdown hook in general to do work just before the jvm exits, no need for these listeners.
Shutdown Hook
If you want to do it by listener, use the windowClosedEvent:
#Override
public void windowClosed(WindowEvent e) {
save();
}
By default the last frame exits the application, unless you did not set the default close operation to anything other than DISPOSE_ON_CLOSE.
I am implementing a program which rtp audio to a peer computer and in the same time running a threads to get ping data. Once I click the start button, two method start almost in the same time. The problem is when I want to interrupt/exit the program, the ping data will not show the output but the threads is still running.
Start button
JButton startCap = new JButton("Start");
startCap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent act) {
go();//go() is the rtp audio method
Thread pinging = new Thread() {
public void run() {
PingTest();
}
};
pinging.setName("runPing");
pinging.start();
}
});
Exit button
JButton stopCap = new JButton("Exit");
stopCap.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent act) {
//just for some checking, I know its not a good design
//I new at here
if (sink != null) {
sink.addDataSinkListener(new DataSinkListener() {
public void dataSinkUpdate(DataSinkEvent e) {
if(e instanceof EndOfStreamEvent)
sink.close();
}
});
}
if (processor != null)
processor.stop();
System.exit(-1);
//I think there no need to return, but still put at there
Thread.currentThread().interrupt();//preserve the message
pinging.currentThread().interrupt();
return;//
//Runtime.getRuntime().halt(0);
//pinging.stop();
}
});
Some GUI code
public static void createAndShowGui() {
JFrame frame = new JFrame("Sender");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleVoiceTransmiter());
frame.setSize(200, 75);
frame.setVisible(true);
frame.toFront();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
The go() is the main method while PingTest() is the dependent method, when go() start by clicking the "Start" button PingTest() will start. Now the problems is I don't know how to interupt/exit the PingTest(). How can I interrupt/exit all the threads (PingTest()) in the same time? Really need some hints, thanks in advanced.
p/s: PingTest() is using Runtime.exec() to ping the peer computer.
pinging.currentThread() doesn't return the pinging thread. It's equivalent to Thread.currentThread(), since currentThread() is a static method. To get the pinging thread, just use pinging:
pinging.interrupt();
I don't really understand why you would interrupt the current thread either. The current thread is the event dispatch thread, which is running your GUI. There is no reason to interrupt it.
I have made an application that gives the user the option to open up a new spawn of the application entirely. When the user does so and closes the application the entire application terminates; not just the window.
How should I go about recursively spawning an application and then when the user exits the JFrame spawn; killing just that JFrame and not the entire instance?
Here is the relevant code:
[...]
JMenuItem newMenuItem = new JMenuItem ("New");
newMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new MainWindow();
}
});
fileMenu.add(newMenuItem);
[....]
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
fileMenu.add(exit);
[...]
First you should try JFrame.DISPOSE_ON_CLOSE instead of JFrame.EXIT_ON_CLOSE because EXIT_ON_CLOSE shuts down the application no matter if there are active threads running or not.
If you still have issues you should consider to introduce an instance counter. To react smarter
See also this discussion
I completely removed the frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE); code.
I changed it to DISPOSE_ON_CLOSE and still the problem was still happening. I ended up creating a windowEvent and adding: frame.dispose(); and the behavior is what I wanted.
Here is the code:
frame.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent e) {
//Allows for multiple instances and properly closing
//only one of the Frames instead of all of them
frame.dispose();
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
});