Results 1 to 20 of 20
  1. #1
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Exclamation dispose() method not working

    Hello everyone. I've created a couple of JFrames each with a "close" button which runs the method dispose(). The "close" only works on one JFrame yet fails to function on all the others.

    Snippet of functioning dispose():
    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class showAllRecordsGUI extends JFrame implements ActionListener{
    	
    	JLabel headerLabel;
    	JTextField results;
    	JButton sortByPrice;
    	JButton sortByExpiry;
    	JButton sortByName;
    	JButton reset;
    	JButton close;
    	
    	public showAllRecordsGUI(){
    		
    		super("Show All Records");
    		setSize(600, 700);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			
    		Container somewhere = getContentPane();
    		somewhere .setSize(600,700);
    		somewhere .setBackground(Color.white); 
    		somewhere .setLayout(null);
    			
    		headerLabel = new JLabel("Show All Records");
    			
    		Color pink = new Color(255, 00, 255);							
    		Font sansSerif = new Font("sans serif", Font.BOLD, 24);     
    		headerLabel.setFont(sansSerif);							
    		headerLabel.setForeground(pink); 
    			
    		results = new JTextField("");
    		sortByPrice = new JButton("Sort by price");
    		sortByExpiry = new JButton ("Sort by expiry");
    		sortByName = new JButton ("Sort by name");
    		reset = new JButton ("Reset");
    		close = new JButton ("Close");;
    			
    		addXY(somewhere, headerLabel, 180, 10, 410, 100);
    		addXY(somewhere, results, 40, 140, 500, 350);
    		addButtonXY(somewhere, sortByPrice, 40, 500, 150, 50);
    		addButtonXY(somewhere, sortByExpiry, 200, 500, 150, 50);
    		addButtonXY(somewhere, sortByName, 360, 500, 150, 50);
    		addButtonXY(somewhere, reset, 40, 600, 150, 50);
    		addButtonXY(somewhere, close, 200, 600, 150, 50);
    			
    		setContentPane(somewhere);
    	}
    	
    	void addButtonXY(Container c, JButton cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		cp.addActionListener(this);
    	 	c.add(cp);	
    	}
    	
    	void addXY(Container c, Component cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		c.add(cp);
    	}
    	
    	public void actionPerformed(ActionEvent click){
    		
    		if (click.getSource() == sortByPrice){
    			//method;
    		}
    		
    		if (click.getSource() == sortByExpiry){
    			//method();
    		}
    		
    		if (click.getSource() == sortByName){
    			//method();
    		}
    		
    		if (click.getSource() == reset){
    			//method();
    		}
    		
    		if (click.getSource() == close){
    			dispose();
    		}
    	}
    	
    	void message(String message, String header){
    		JOptionPane.showMessageDialog(null,message,header,JOptionPane.ERROR_MESSAGE); 
    	}
    	
    	int checkMessage(String message, String header){  
      		return JOptionPane.showConfirmDialog(null,message,header,JOptionPane.YES_NO_OPTION);  
    	}
    }
    Snippet of non functioning dispose():
    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class areYouSureGUI extends JFrame implements ActionListener{
    	
    	JLabel headerLabel;
    	JButton ok;
    	JButton no;
    	
    	public areYouSureGUI(){
    		
    		super("Delete Confirmation");
    		setSize(600,200);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container somewhere = getContentPane();
    		somewhere.setSize(600, 200);
    		somewhere.setBackground(Color.white); 
    		somewhere.setLayout(null);
    		
    		headerLabel = new JLabel("Are you sure you want to delete this periodical?");
    		Color pink = new Color(255, 00, 255);							
    		Font sansSerif = new Font("sans serif", Font.BOLD, 24);     
    		headerLabel.setFont(sansSerif);							
    		headerLabel.setForeground(pink);
    		
    		ok = new JButton ("OK");
    		no = new JButton ("NO");
    		
    		addXY(somewhere, headerLabel, 10, 10, 800, 100);
    		
    		addXY(somewhere, ok, 90, 100, 100,30);
    		addXY(somewhere, no, 280, 100, 100,30);
    		
    		setContentPane(somewhere);
    	}
    	
    	void addButtonXY(Container c, JButton cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		cp.addActionListener(this);
    		c.add(cp);	
    	}
    	
    	void addXY(Container c, Component cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		c.add(cp);
    	}
    	
    	public void actionPerformed(ActionEvent click){
    		
    		if (click.getSource() == ok){
    			//delete
    		}
    		
    		if (click.getSource() == no){
    			dispose();
    		}
    	}
    	
    	void message(String message, String header){
    		JOptionPane.showMessageDialog(null,message,header,JOptionPane.ERROR_MESSAGE);
    	}
    	
    	int checkMessage(String message, String header){  
      		return JOptionPane.showConfirmDialog(null,message,header,JOptionPane.YES_NO_OPTION);  
    	}
    }
    All of the dispose() methods have been written exactly the same way as the functioning dispose().

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    First of all I think you should use a JDialog instead of a JFrame... Second, sorry but I can't see any dispose() method in your code

  3. #3
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    the dispose method is under "public void actionperformed". and thanx for the advice but ill stick with JFrames.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    In the second one, you didn't use addButtonXY(). addXY() doesn't create a listener...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by raffaele181188 View Post
    First of all I think you should use a JDialog instead of a JFrame... Second, sorry but I can't see any dispose() method in your code
    Ya I agreed with you. JFrame cannot dispose. But our thread starter can use setVisible().

  6. #6
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    In the second one, you didn't use addButtonXY(). addXY() doesn't create a listener...
    Yes i did use addButtonXY(). Its right above addXY().

  7. #7
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ya I agreed with you. JFrame cannot dispose. But our thread starter can use setVisible().
    I tried replacing "dispose()" with "setVisible(false)" but nothing happens. I'm struggling to spot the error here, all the "dispose()" are written exactly the same way. How come only the one in the file "showAllRecordsGUI.class" works?

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

    Default

    To Eranga: What do you mean by "JFrame cannot dispose."? JFrames can dispose() just fine, but maybe I'm misinterpreting what you stated.

    To the original poster, if you haven't done so already, you should do some debugging to see if your actionperformed is even being called the way you think it is. I would place a
    Java Code:
    if (click.getSource() == no) {
      System.out.println("is this being called");
      dispose();
    }
    in your actionPerformed method to see if the dispose line is even being reached. For one thing, I don't see where you've added an ActionListener to the "no" JButton.

    Best of luck!

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    First, dispose() is invoked in the handlers, against "this", but "this" is explicitly stated.

    ok = new JButton ("OK");
    no = new JButton ("NO");

    addXY(somewhere, headerLabel, 10, 10, 800, 100);

    addXY(somewhere, ok, 90, 100, 100,30);
    addXY(somewhere, no, 280, 100, 100,30);
    You defined addButtonXY(), but you didn't use it...

  10. #10
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    To the original poster, if you haven't done so already, you should do some debugging to see if your actionperformed is even being called the way you think it is. I would place a
    Java Code:
    if (click.getSource() == no) {
      System.out.println("is this being called");
      dispose();
    }
    in your actionPerformed method to see if the dispose line is even being reached. For one thing, I don't see where you've added an ActionListener to the "no" JButton.

    Best of luck!
    I invoked the print statement you suggested and found out that dispose() is not being reached i.e. the print statement is not executed.

    Regarding adding an ActionListener, I have no idea how to do that. The way I've done it is the way we've been taught. I've reviewed some of my peers' codes with dispose() methods that work and they've written it exactly the same way as I have! (of course the variable names are different but that shouldn't matter)

  11. #11
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    First, dispose() is invoked in the handlers, against "this", but "this" is explicitly stated.


    You defined addButtonXY(), but you didn't use it...
    I added "Button" in front of "add" before "XY" where appropriate. Still, it didn't work.
    I didn't quite get what you're trying to say about "this" and "handlers".

    You mentioned not using addButtonXY in your previous reply but i misunderstood what you were saying. My fault.
    Last edited by R&R; 01-11-2009 at 02:17 PM.

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb this and that

    I got interested in the thread because I had seen dispose() called on a graphics ( code I wrote a few days ago. ) It would seem obvious to me that a dispose(), callable against whatever system resources had been allocated, should be available in the api. That is what in essence fubarable is saying implicitly -> the code generated by the compiler *should* "dispose" system resources, and such a release would be an intrinsic, something understood to be occuring "right there" ~ and so much so that the compiler would generate code and install it in the execution path blindside.

    That is the first reason, and today stands as the primary and principaled Goddess of Goodness and Godzilla of Code Correctness, Prima-Facia, Unquestionable in my code: All variable names in my code have no human interperable connotations. The reason: some system coder somewhere may ... ...MAY... have coded a method call by the same name for the same reason. Thus, at that point, you do not have an override - you have a blunder.

    Pending discussion - that is the general area fubarable is talking about.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  13. #13
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default One more time

    OK. About "this". That was in response to Eranga, who didn't see you call dispose() anywhere. I was simply saying that you did indeed call dispose().

    Here's your code, with some highlighting. Bold to draw attention to what you have, bold italics to indicate my changes.

    Java Code:
    		ok = new JButton ("OK");
    		no = new JButton ("NO");
    		
    		addXY(somewhere, headerLabel, 10, 10, 800, 100);
    		
    		add[B][I]Button[/I][/B]XY(somewhere, ok, 90, 100, 100,30);
    		add[B][I]Button[/I][/B]XY(somewhere, no, 280, 100, 100,30);
    		
    		setContentPane(somewhere);
    	}
    	
    	void addButtonXY(Container c, JButton cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		[B]cp.addActionListener(this);[/B]
    		c.add(cp);	
    	}
    	
    	void addXY(Container c, Component cp, int x, int y, int w, int h){
    		cp.setBounds(x,y,w,h);
    		c.add(cp);
    	}
    	
    	[B]public void actionPerformed(ActionEvent click)[/B]{
    		[B][I]System.out.println("In actionPerformed.");[/I][/B]
    		if (click.getSource() == ok){
    			//delete
    		}
    		
    		if (click.getSource() == no){
    			[B]dispose();[/B]
    		}
    	}
    From what I can see, your code should work, if you made the changes to use addButtonXY that I indicated. I also added a println() for debugging. It will tell you if your handler is invoked.

    I also highlighted the code where you added a handler to the buttons, the handler itself, and the dispose() call.

  14. #14
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    From what I can see, your code should work, if you made the changes to use addButtonXY that I indicated. I also added a println() for debugging. It will tell you if your handler is invoked.

    After altering addButtonXY (as you've clearly stated), I added the print statment under actionPerformed in the JFrame with the non-working dispose() and nothing was printed.
    However, I also added the print statement under actionPerformed in the JFrame with the functioning dispose() and surprisingly didn't receive any output. Weird.

  15. #15
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Is the frame at least disposing???

    If you are running from a command line in Windows, there are two commands, java and javaw. The difference is that javaw does not show console output in the command window. I don't know what happens in other OS's...

    You can use System.setOut(PrintStream) and System.setErr(PrintStream) to point to files in a temp directory, so that you don't lose your output. In fact, that's a good start on a simple logging system.

  16. #16
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    Is the frame at least disposing???
    No. It still refuses to dispose itself.

  17. #17
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Here's the class, slightly rewritten

    I copied your class, but I renamed it and added a main. I also created an anonymous inner class for the handler and had it call actionPerformed, which I renamed handleClick. Having one class be a JFrame and a mouse event handler isn't ideal OO design, and, if you add another button, your actionPeformed method will end up getting messy. Last, I made the change to use addButtonXY() for the buttons.

    It disposes fine.
    Java Code:
    package test;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    public class DisposeTest
        extends
        javax.swing.JFrame {
    	private static final long serialVersionUID = 7164131950441569550L;
    
    	public static void main(String[] args) {
    		final DisposeTest instance = new DisposeTest();
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				instance.setVisible(true);
    			}
    		});
    	}
    
    	JLabel headerLabel;
    	JButton ok;
    	JButton no;
    
    	public DisposeTest() {
    		super("Delete Confirmation");
    		setSize(600,
    		    200);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container somewhere = getContentPane();
    		somewhere.setSize(600,
    		    200);
    		somewhere.setBackground(Color.white);
    		somewhere.setLayout(null);
    		headerLabel = new JLabel("Are you sure you want to delete this periodical?");
    		Color pink = new Color(255,
    		    00,
    		    255);
    		Font sansSerif = new Font("sans serif",
    		    Font.BOLD,
    		    24);
    		headerLabel.setFont(sansSerif);
    		headerLabel.setForeground(pink);
    		ok = new JButton("OK");
    		no = new JButton("NO");
    		addXY(somewhere,
    		    headerLabel,
    		    10,
    		    10,
    		    800,
    		    100);
    		addButtonXY(somewhere,
    		    ok,
    		    90,
    		    100,
    		    100,
    		    30);
    		addButtonXY(somewhere,
    		    no,
    		    280,
    		    100,
    		    100,
    		    30);
    		setContentPane(somewhere);
    	}
    
    	void addButtonXY(Container c,
    	    JButton cp,
    	    int x,
    	    int y,
    	    int w,
    	    int h) {
    		cp.setBounds(x,
    		    y,
    		    w,
    		    h);
    		cp.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent pE) {
    				handleClick(pE);
    			}
    		});
    		c.add(cp);
    	}
    
    	void addXY(Container c,
    	    Component cp,
    	    int x,
    	    int y,
    	    int w,
    	    int h) {
    		cp.setBounds(x,
    		    y,
    		    w,
    		    h);
    		c.add(cp);
    	}
    
    	public void handleClick(ActionEvent click) {
    		if (click.getSource() == ok) {
    			//delete
    		}
    		if (click.getSource() == no) {
    			dispose();
    		}
    	}
    
    	void message(String message,
    	    String header) {
    		JOptionPane.showMessageDialog(null,
    		    message,
    		    header,
    		    JOptionPane.ERROR_MESSAGE);
    	}
    
    	int checkMessage(String message,
    	    String header) {
    		return JOptionPane.showConfirmDialog(null,
    		    message,
    		    header,
    		    JOptionPane.YES_NO_OPTION);
    	}
    }

  18. #18
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    =Unquestionable in my code: All variable names in my code have no human interperable connotations.
    What do you mean by "human interperable connotations"?

  19. #19
    R&R
    R&R is offline Member
    Join Date
    Nov 2008
    Location
    Oman
    Posts
    12
    Rep Power
    0

    Default

    I know it's been a very very long time but thanks to all of you. I managed to fix my problem =)

    Cheers

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [SOLVED] Not dispose my JPanel
    By Eranga in forum Advanced Java
    Replies: 3
    Last Post: 11-14-2010, 04:02 PM
  2. Can't find out how to get this method working :)
    By Shadaw in forum Java Applets
    Replies: 2
    Last Post: 12-29-2008, 06:35 PM
  3. [SOLVED] how do I dispose a view?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-07-2008, 07:15 PM
  4. makeButton method not working
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-10-2008, 11:20 PM
  5. How to dispose tooltip.
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-22-2008, 08:06 AM

Posting Permissions

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