Results 1 to 6 of 6
  1. #1
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Unhappy closing JFrame without terminating program

    There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating the program but I'm having no luck. This is the code that I'm trying to use for the second frame, which without the button compiles fine:

    Java Code:
    class Time_First_Depot extends JFrame
    {	
    	Time_First_Depot()
    	{
    		Container c = getContentPane(); \\ creates content pane
    		c.setLayout ( null );
    		
    		Color b = new Color(100,200,255); \\ set colour of JFrame 
    		c.setBackground( b );
    		
    		JButton exit = new JButton("EXIT"); \\creats button
    		exit.addActionListener(new ExitButtonListener()); \\ adds listener to button
    		
    		exit.setForeground(Color.BLUE); \\edits buton
    		exit.setFont(new Font("Time", Font.BOLD, 12));
    		
    		c.add(exit);\\adds button
    		
    		exit.setBounds(250, 375, 90, 30);\\ sets location of button
    		
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450);  // set position and size
    		this.setResizable(false);
    		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
    		this.setTitle("Time");
    		this.setVisible(true);
    		this.setResizable(false);
    	}
    }
    
    class ExitButtonListener implements ActionListener
    {
    	ExitButtonListener(){}
                public void actionPerformed(ActionEvent e)
                {
                	    if (e.getActionCommand().equals("EXIT"))
                	    {
    
                	    	    this.dispose();
                	    }
                }
    }

    however, when i try to compile it the following message comes up:


    cannot find symbol
    symbol : method dispose()
    location: class ExitButtonListener
    this.dispose();
    Last edited by chirag123; 04-24-2011 at 08:23 PM.

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

    Default

    Your solution: Don't use a second JFrame but rather have the second window be a JDialog since it is truly acting as a dialog of the first window. And to answer your next question, yes, a JDialog can hold as complex a GUI as any JFrame.


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  3. #3
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for that Fubarable.

    I know this may sound silly but I'm new to java. I'm not sure what you mean by dialog (excuse my inexperience). My teachers brief is to create a program for a bus company. The first window is basically the main menu which has links to the other frames. Not sure how that impacts on dialog but thought perhaps some more information may help clarify things.

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

  5. #5
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for that after reading that I understand what a dialog box is but that's not quite what I'm trying to achieve. The code that I pasted is supposed to give the time from one bus depot to another bus depot, when the user selects using buttons which depots they start at and finish at and that part of the code is functioning fine so I didn't bother copying needless coding which isn't relevant to the question. Looking back perhaps I should have and it would have been clearer

  6. #6
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for your help, thought I'd post the solution which may help other people:

    Java Code:
    		JButton exit = new JButton("EXIT");
    		exit.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				dispose();
    			}
    		});

Similar Threads

  1. Closing a JFrame. setVisible etc ain't workin'.
    By AndroidAppNewbie in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 05:54 PM
  2. Closing a JFrame
    By SBL in forum AWT / Swing
    Replies: 5
    Last Post: 11-23-2009, 02:54 PM
  3. Closing Popup JFrame in Applet
    By Arsenic in forum Java Applets
    Replies: 3
    Last Post: 04-10-2009, 08:37 PM
  4. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:17 AM

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
  •