Results 1 to 15 of 15
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default Problems with exiting application

    Hello,

    I have been creating a GUI and there is an exit JButton that can be clicked and when it is clicked the screen disappears and I thought that the application had exited, but after running it a few times I noticed that my RAM had gone up to more than usual for what I have running on my system. So I opened up my task manager and noticed that there were many instances of javaw.exe running. I know one of them is eclipse and I guess that the others are from where I repeatedly test my application and it isn't closing like I thought it was when I click the exit JButton. If I use the windows close button I don't have the same problem, it only happens when I use the JButton to close. I guess that just by using setVisible(false); to set the visibility of the window to false is not enough and I need to find another command to actually exit rather than making it invisible. I'm sure one of you intelligent folk will see at a glance what iswrong. I'm still new to Java so I haven't got a clue what I should use instead.

    Here is the code for the handler of the exit JButton.

    Java Code:
    exit.addActionListener(
    	new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			setVisible(false);
    		}
    	}
    );

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    You're making the frame invisible but its still running.

    Instead use:

    Java Code:
    System.exit(1);

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would call dispose() on the JFrame and be sure to set its setDefaultCloseOperation to whatever I desired for that window, JFrame.EXIT_ON_CLOSE if you desire to exit at the time of closing.

  4. #4
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    Thanks for getting back so quickly, just tested it and it works. no more excessive RAM usage :)

  5. #5
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    My thanks was for solarsonic but now I have a question for Fubarable.

    so by calling dispose(); Java would automatically call the default close operation for that frame?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    My thanks was for solarsonic but now I have a question for Fubarable.

    so by calling dispose(); Java would automatically call the default close operation for that frame?
    Try it and see! :)

  7. #7
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    I have just tested dispose(); in place of System.exit(1); but it fails to terminate the process, only the frame disappears which is the same result that I had originally.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    I have just tested dispose(); in place of System.exit(1); but it fails to terminate the process, only the frame disappears which is the same result that I had originally.
    show the code with code tags so we can see. It works fine for me.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Calling System.exit() is fine for simple programs. But keep in mind that if you have any resources open (files, sockets, etc.) it would be better to have your close button call a method that closes everything down neatly. Likewise, write an event handler for clicks on the JFrame's close button and have it call that same method.

  10. #10
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default Here is the full code

    Here is the code for the login screen, as you can see, I have used both the System.exit(1); for the exit button and dispose(); for the cancel button.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LoginScreen extends JFrame {
    	private JLabel usernamelabel = new JLabel("Enter your username:");
    	private JTextField username = new JTextField(20);
    	private JLabel passwordlabel = new JLabel("Enter your password:");
    	private JPasswordField password = new JPasswordField(20);
    	private JButton login = new JButton("Login");
    	private JButton cancel = new JButton("Cancel");
    	private JButton exit = new JButton("Exit");
    	
    	public LoginScreen(){
    		super("Login");
    		setLayout(new FlowLayout());
    		
    		add(usernamelabel);
    		add(username);
    		add(passwordlabel);
    		add(password);
    		add(login);
    		add(cancel);
    		add(exit);
    		
    		login.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						//You still need to build the login process
    						JOptionPane.showMessageDialog(null, "You still need to build the login process");
    					}
    				}
    		);
    		
    		cancel.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						StartScreen startscreen = new StartScreen();
    						startscreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    						startscreen.setSize(240,75);
    						startscreen.setLocationRelativeTo(null);
    						startscreen.setVisible(true);
    						dispose(); //???
    					}
    				}
    		);
    
    		exit.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						System.exit(1);
    					}
    				}
    		);
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, I would think that a login screen would be more of a dialog window rather than an application window. Something else would ask it to pop up, and if opened, nothing else in the application would be accessible. Then if the user presses exit, I would have the main application get this information from the login object, and then the main app would handle the exiting.

  12. #12
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    Would I do that by setting an exit variable to 1 when the exit button is clicked and then getting the main application to check to see if the login objects exit variable is set to 1? I'm still new to Java but it's slowly starting to sink in.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    Would I do that by setting an exit variable to 1 when the exit button is clicked and then getting the main application to check to see if the login objects exit variable is set to 1? I'm still new to Java but it's slowly starting to sink in.
    You could, but I would avoid so-called "magic numbers". You could steal some Java constants, for example I sometimes have used JOptionPane constants even when not using JOptionPanes, or I've created my own enums for something like this. For e.g.,

    Java Code:
    public enum LoginState {
       LOGIN, EXIT, CANCEL
    }


    Java Code:
    public class LoginScreen2 extends JPanel {
       public static final LoginState DEFAULT_LOGIN_STATE = LoginState.CANCEL;
       private LoginState loginState = DEFAULT_LOGIN_STATE;
       private String userName;
       private char[] password;
    
       public LoginState getLoginState() {
          return loginState;
       }
    
       public String getUserName() {
          return userName;
       }
    
       public char[] getPassword() {
          return password;
       }
    
       // etc.....
    
    }

  14. #14
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    To be honest, that didn't really make much sense to me, but thanks for your help, I'm thinking I need to do a bit of research into enums. I did watch a couple of tutorials about enums on youtube but not enough to understand what you just did. It would be nice to get my knowledge of Java to the point where I know what to do to achieve almost everything. I'm off to learn enums.

    peace out :)

  15. #15
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by JohnPringle83 View Post
    I have just tested dispose(); in place of System.exit(1); but it fails to terminate the process, only the frame disappears which is the same result that I had originally.
    It fails to terminate the process because you didn't follow fubarable's instructions "be sure to set its [the frame's] setDefaultCloseOperation to whatever you desire for that window, JFrame.EXIT_ON_CLOSE if you desire to exit at the time of closing".

Similar Threads

  1. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  2. Application server problems
    By bbq in forum Advanced Java
    Replies: 3
    Last Post: 12-31-2010, 02:48 PM
  3. Exiting a while loop using a String?
    By Isshin in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:33 PM
  4. exiting a while loop with a string
    By Aldius in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 10:38 PM
  5. Exiting on menuSelected action
    By benra in forum New To Java
    Replies: 8
    Last Post: 10-13-2009, 04:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •