Results 1 to 20 of 20
Thread: dispose() method not working
- 01-09-2009, 06:01 AM #1
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
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():
Snippet of non 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); } }
All of the dispose() methods have been written exactly the same way as the 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); } }
Any help would be greatly appreciated.
- 01-09-2009, 10:35 AM #2
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
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
- 01-09-2009, 04:37 PM #3
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
the dispose method is under "public void actionperformed". and thanx for the advice but ill stick with JFrames.
- 01-09-2009, 07:20 PM #4
In the second one, you didn't use addButtonXY(). addXY() doesn't create a listener...
- 01-10-2009, 02:17 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-10-2009, 02:02 PM #6
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
- 01-10-2009, 02:05 PM #7
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
-
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
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.Java Code:if (click.getSource() == no) { System.out.println("is this being called"); dispose(); }
Best of luck!
- 01-10-2009, 10:48 PM #9
First, dispose() is invoked in the handlers, against "this", but "this" is explicitly stated.
You defined addButtonXY(), but you didn't use it...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);
- 01-11-2009, 01:12 PM #10
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
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)
- 01-11-2009, 01:15 PM #11
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
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 01:17 PM.
- 01-11-2009, 04:28 PM #12
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
- 01-11-2009, 11:47 PM #13
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.
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.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] } }
I also highlighted the code where you added a handler to the buttons, the handler itself, and the dispose() call.
- 01-12-2009, 02:32 PM #14
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
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.
- 01-12-2009, 03:04 PM #15
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.
- 01-13-2009, 02:42 PM #16
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
- 01-13-2009, 05:33 PM #17
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); } }
- 05-13-2009, 02:24 PM #18
What do you mean by "human interperable connotations"?=Unquestionable in my code: All variable names in my code have no human interperable connotations.
- 11-14-2010, 05:37 PM #19
Member
- Join Date
- Nov 2008
- Location
- Oman
- Posts
- 12
- Rep Power
- 0
I know it's been a very very long time but thanks to all of you. I managed to fix my problem =)
Cheers
- 11-15-2010, 01:46 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How long you take to solve the problem. ;)
Anyway please mark the thread solved then. It's really helpful to all other members.
Similar Threads
-
[SOLVED] Not dispose my JPanel
By Eranga in forum Advanced JavaReplies: 3Last Post: 11-14-2010, 03:02 PM -
Can't find out how to get this method working :)
By Shadaw in forum Java AppletsReplies: 2Last Post: 12-29-2008, 05:35 PM -
[SOLVED] how do I dispose a view?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-07-2008, 06:15 PM -
makeButton method not working
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08-10-2008, 10:20 PM -
How to dispose tooltip.
By Preethi in forum New To JavaReplies: 1Last Post: 07-22-2008, 07:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks