I have a JFrame that, when I click "new" in the menu bar, creates
another JFrame like that of the first. It just runs the following code
....
EventQueue.invokeLater(new Runnable() {
public void run() {
MyApp app = new MyApp();
app.createGUI();
}
});
And inside the createGUI() method is this ...
addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
As it works right now, when I close a window, all the windows close and
I'd just like to close only that specific window in which the user
clicked the close button.
Any help much appreciated.
|
|
0
|
|
|
|
Reply
|
Wizumwalt (114)
|
12/18/2006 10:26:59 PM |
|
William Z. wrote:
> I have a JFrame that, when I click "new" in the menu bar, creates
> another JFrame like that of the first. It just runs the following code
> ...
>
> EventQueue.invokeLater(new Runnable() {
> public void run() {
> MyApp app = new MyApp();
> app.createGUI();
> }
> });
>
> And inside the createGUI() method is this ...
>
> addWindowListener(new WindowListener() {
> public void windowOpened(WindowEvent arg0) {}
> public void windowClosed(WindowEvent arg0) {}
> public void windowIconified(WindowEvent arg0) {}
> public void windowDeiconified(WindowEvent arg0) {}
> public void windowActivated(WindowEvent arg0) {}
> public void windowDeactivated(WindowEvent arg0) {}
> public void windowClosing(WindowEvent arg0) {
> System.exit(0);
> }
> });
>
> As it works right now, when I close a window, all the windows close and
> I'd just like to close only that specific window in which the user
> clicked the close button.
>
> Any help much appreciated.
The System.exit(0); call tells the JVM to terminate the program....
Don't use the addWindowListener at all, instead what you really want is
to call
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Hope this helps,
Daniel.
|
|
0
|
|
|
|
Reply
|
Daniel
|
12/18/2006 11:03:27 PM
|
|
But that would just close the windows and leave the VM running. Should
I keep a running count of open frames and when 0, call System.exit()?
>
> The System.exit(0); call tells the JVM to terminate the program....
> Don't use the addWindowListener at all, instead what you really want is
> to call
> setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
>
> Hope this helps,
> Daniel.
|
|
0
|
|
|
|
Reply
|
William
|
12/18/2006 11:31:34 PM
|
|
William Z. wrote:
> > The System.exit(0); call tells the JVM to terminate the program....
> > Don't use the addWindowListener at all, instead what you really want is
> > to call
> > setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
> >
> > Hope this helps,
> > Daniel.
> But that would just close the windows and leave the VM running. Should
> I keep a running count of open frames and when 0, call System.exit()?
Please don't top post... Makes following conversations difficult. :-)
The Event Dispatch Thread (EDT) should exit when all JFrames have been
disposed, and the JVM should exit when all the non-daemon (including
the EDT) threads exit.
So, you shouldn't have to keep track of all open frames.
|
|
0
|
|
|
|
Reply
|
Daniel
|
12/19/2006 12:09:01 AM
|
|
William Z. wrote:
> I have a JFrame that, when I click "new" in the menu bar, creates
> another JFrame like that of the first. It just runs the following code
> ...
>
> EventQueue.invokeLater(new Runnable() {
> public void run() {
> MyApp app = new MyApp();
> app.createGUI();
> }
> });
>
> And inside the createGUI() method is this ...
>
> addWindowListener(new WindowListener() {
> public void windowOpened(WindowEvent arg0) {}
> public void windowClosed(WindowEvent arg0) {}
> public void windowIconified(WindowEvent arg0) {}
> public void windowDeiconified(WindowEvent arg0) {}
> public void windowActivated(WindowEvent arg0) {}
> public void windowDeactivated(WindowEvent arg0) {}
> public void windowClosing(WindowEvent arg0) {
> System.exit(0);
> }
> });
>
> As it works right now, when I close a window, all the windows close and
> I'd just like to close only that specific window in which the user
> clicked the close button.
>
> Any help much appreciated.
>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener {
JButton openButton,closeButton;
JFrame frame;
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
openButton = new JButton("Open");
openButton.addActionListener(this);
add(openButton);
closeButton = new JButton("Close");
closeButton.setEnabled(false);
closeButton.addActionListener(this);
add(closeButton);
frame = new JFrame("extra frame");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
test.this.actionPerformed(new ActionEvent(
closeButton,ActionEvent.ACTION_PERFORMED,"Close"));
}
});
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Open")) {
openButton.setEnabled(false);
frame.setVisible(true);
closeButton.setEnabled(true);
} else if (ac.equals("Close")) {
closeButton.setEnabled(false);
frame.setVisible(false);
openButton.setEnabled(true);
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
new test();
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/
|
|
0
|
|
|
|
Reply
|
Knute
|
12/19/2006 3:29:59 AM
|
|
|
4 Replies
121 Views
(page loaded in 2.3 seconds)
Similiar Articles: How to save files into a specific folder - comp.soft-sys.matlab ...... return; end else return; end end try mov = aviread(movieFullFileName); % movie(mov); % Determine how many frames ... How to save files into a specific folder ... Howto: Simulate a close (X) button in an applications toolbar ...Can JDialogs or JInternal frames have close boxes? -- Canadian Mind Products, Roedy ... Out of memory, close specific figure on GUI - comp.soft-sys.matlab ... Howto ... JFrame intercepting keystrokes - comp.lang.java.guiIs it possible to have the JFrame respond to certain key-strokes.. ie. ctrl-1, f1, etc, etc? ... Another example of what I'm after, is a dialog box that will close when ... Open new JPanel (JFrame, ...) by clicking JButton - comp.lang.java ...I tried to use it with the above tip, and needed to include the creation of a new JFrame (what you suggested me: thanks!). Only closing a JFrame seems to be a problem? Panel text not showing in JFrame - comp.lang.java.gui... 75); // position frame myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ... to be 10 pixels wider around then the panel. The JFrame ... Replace Text on Specific PDF Page... Guide: Set handles out of closerequestfcn - comp.soft-sys.matlab ...The goal is to enable the user to read several excelfiles subsequently and to ... Out of memory, close specific figure on GUI - comp.soft-sys.matlab ... Guide: Set handles ... extracting frames from video - comp.soft-sys.matlaboutputFolder = fullfile(cd, 'Rhino Movie'); if ~exist(outputFolder, 'dir') mkdir(outputFolder); end % Determine how many frames there are. Calculating folder size - comp.sys.mac.systemAnother space will close the display (faster than ... How to save files into a specific folder - comp.soft-sys ... 14; % Change the current folder ... how many frames there ... display video in a GUI - comp.soft-sys.matlab... No', 'Yes'); if strcmp(button, 'No') return; end % Read the frame back ... aviread(movieFullFileName); > % movie(mov); > % Determine how many frames ... how to get frames from .avi format. - comp.soft-sys.matlab ...... movie(mov); % Determine how many frames there ... Processed frame %4d of %d.', frame, numberOfFrames); end disp ... How to save files into a specific folder ... JFrame (Java 2 Platform SE v1.4.2)EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. ... This is only a hint, as certain look and feels may not support this feature. JFrame (Java Platform SE 6)EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. ... This is only a hint, as certain look and feels may not support this feature. 7/29/2012 11:50:42 AM
|