Dispose a JFrame using JPanel

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 3
Reputation: kathmartir is an unknown quantity at this point 
Solved Threads: 0
kathmartir kathmartir is offline Offline
Newbie Poster

Dispose a JFrame using JPanel

 
0
  #1
Oct 6th, 2009
I have a problem with the dispose method. . .I got 3 java files with a class name:

MainProgram.java
MainPanel.java
PageOne.java

The MainProgram java extends JFrame.
The MainPanel.java extends JPanel.
The PageOne.java extends JFrame.

The MainPanel.java is where all the actions and etc. are being set.
The MainProgram.java calls the MainPanel.java to display what is in the MainPanel.java.
All of these works good until I put the dispose(); method on one of the condition. All I want is to close the MainProgram.java before calling or opening the PageOne.java. I thought the use of method dispose(); would be same as using it in JFrame. Can you give a good idea on how to do this? I am using mouseListener on my action.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,714
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #2
Oct 6th, 2009
When I want to go from one frame to another I use:
  1. JFrame.setVisible(true/false);

If you call it with false, the frame is not lost. It is just not visible. Meaning that you can call back the same method and set it to visible(true) whenever you want and the frame will reappear
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 16
Reputation: sincerelibran is an unknown quantity at this point 
Solved Threads: 9
sincerelibran sincerelibran is offline Offline
Newbie Poster
 
0
  #3
Oct 6th, 2009
use this
MainProgram mp = new MainProgram();
mp.setVisible(false);

This way you can hide the MainProgram JFrame. Don't think of disposing it. This is another alternative.

Regards

Sincerelibran

Originally Posted by kathmartir View Post
I have a problem with the dispose method. . .I got 3 java files with a class name:

MainProgram.java
MainPanel.java
PageOne.java

The MainProgram java extends JFrame.
The MainPanel.java extends JPanel.
The PageOne.java extends JFrame.

The MainPanel.java is where all the actions and etc. are being set.
The MainProgram.java calls the MainPanel.java to display what is in the MainPanel.java.
All of these works good until I put the dispose(); method on one of the condition. All I want is to close the MainProgram.java before calling or opening the PageOne.java. I thought the use of method dispose(); would be same as using it in JFrame. Can you give a good idea on how to do this? I am using mouseListener on my action.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: kathmartir is an unknown quantity at this point 
Solved Threads: 0
kathmartir kathmartir is offline Offline
Newbie Poster
 
0
  #4
Oct 7th, 2009
I use the setVisible method, still it won't work. . .try to look at the code below:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4.  
  5. public class MainPage extends JFrame {
  6. private MainPagePanel mpp;
  7.  
  8. public MainPage() {
  9. super("Test");
  10. setSize(180,100);
  11. setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  12. mpp = new MainPagePanel();
  13. Container pane = getContentPane();
  14. pane.add(mpp);
  15. setContentPane(mpp);
  16. setResizable(false);
  17. setVisible(true);
  18. }
  19.  
  20. public static void main(String[] args) {
  21.  
  22. MainPage mp = new MainPage();
  23. }
  24. }

  1. import java.awt.*;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.event.MouseListener;
  4. import javax.swing.*;
  5.  
  6. public class MainPagePanel extends JPanel {
  7.  
  8. ImageIcon image;
  9. JLabel display;
  10.  
  11. public MainPagePanel() {
  12. Toolkit kit = Toolkit.getDefaultToolkit();
  13.  
  14. image = new ImageIcon("menu1.jpg");
  15. display = new MapLabel(image, "Display");
  16. display.setBounds(10, 20, image.getIconWidth(), image.getIconHeight());
  17.  
  18. setPreferredSize(new Dimension(457, 540));
  19. setLayout(null);
  20. add(display);
  21.  
  22. }
  23.  
  24. class MapLabel extends JLabel implements MouseListener {
  25. Icon icon;
  26. String name;
  27. MainPage mp; // Declaring MainPage mp = new MainPage(); here will result to error
  28.  
  29. public MapLabel(Icon icon, String name) {
  30. this.icon = icon;
  31. this.name = name;
  32.  
  33. setIcon(icon);
  34. setToolTipText(name);
  35. addMouseListener(this);
  36. }
  37.  
  38. public void mouseClicked(MouseEvent e) {
  39. String image = name;
  40.  
  41. if(image.equals("Display")) {
  42. // How can I close the MainPage program if I click the display image label before PageOne will appear?
  43. // setVisibility won't work.
  44. PageOne po = new PageOne();
  45. mp.setVisible(false); // disposing PageOne
  46. mp.dispose(); //disposing PageOne
  47. mp = new MainPage();
  48. }
  49.  
  50. }
  51. public void mouseEntered(MouseEvent e) {
  52. setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  53. }
  54. public void mouseExited(MouseEvent e) {
  55. setCursor(null);
  56. }
  57. public void mousePressed(MouseEvent e) {}
  58. public void mouseReleased(MouseEvent e) {}
  59. }
  60. }

I have a problem in the if condition and also in declaring the MainPage mp = new MainPage();

Here is the whole to program to test. . .
Attached Files
File Type: zip sample problem.zip (7.4 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
0
  #5
Oct 7th, 2009
this instruction is ambigous: addMouseListener(this); it's similar to this.addMouseListener(this);
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,714
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
1
  #6
Oct 7th, 2009
From what I see you use a lot of "extending" that I don't think is necessary. Your code works but it could have been simpler.
First don't use the "dispose" method.

Second you need to close the the MainPage and open the PageOne.
To do that you need to call the setVisible(false) on the MainPage that is open. In order to have access to the original MainPage that you opened you need to pass it as parameter to the MapLabel.
But MapLabel is called from the MainPagePanel so that also needs to have it as parameter:

MapLabel class
MainPage mp; 

public MapLabel(Icon icon, String name, MainPage mp) {

   this.mp = mp;
}

// AT THE LISTENER:

......
PageOne po = new PageOne();
mp.setVisible(false); // disposing PageOne

// mp = new MainPage(); // you don't need that. It will create a new MainPage and overwrite the existing one.
// you have already created a MainPage, don't create another


But in order to call this:
public MapLabel(Icon icon, String name, MainPage mp) you need:

MainPagePanel class
MainPage mp;

public MainPagePanel(MainPage mp) {
		Toolkit kit = Toolkit.getDefaultToolkit();

		image = new ImageIcon("menu1.jpg");
                this.mp = mp;
display = new MapLabel(image, "Display", mp);


And now to pass the MainPage that is opened as paramater

MainPage class
public MainPage() {
		super("Test");
		setSize(180,100);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

// "this" is a MainPage. This MainPage that you just created and has all your Panels and labels
		mpp = new MainPagePanel(this);

		Container pane = getContentPane();
		pane.add(mpp);
		setContentPane(mpp);
		setResizable(false);
		setVisible(true);
	}


With that way, "this", is in all the other Panels and Labels and they have access to the same MainPage that you just opened.
Now that you have it inside you can pass it to the PageOne class and after you are done the PageOne can set it back to visible true while setting itself to false:

class PageOne
  1. class PageOne {
  2. MainPage mp;
  3.  
  4. public PageOne(MainPage mp) {
  5. this.mp = mp;
  6. }
  7.  
  8. ....
  9. ...
  10. // in some method:
  11. {
  12. this.setVisble(false); //hiding PageOne
  13. mp.setVisible(true);
  14. }
  15. }

It is better to call dispose on the PageOne:
  1. mp.setVisible(true);
  2. this.dispose();

Because you created it locally inside the Label and no one else has access to it, so if it is disposed no one will be affected by it.
Also whenever you call "Display" you create a new one, so if you set it to visible false, you will keep creating new PageOne objects that afterward will be simply not visible.
If you had it as a private property of the class and you simply changed its values (not create a new one) thetn it would be OK
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,714
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #7
Oct 7th, 2009
Originally Posted by moutanna View Post
this instruction is ambigous: addMouseListener(this); it's similar to this.addMouseListener(this);
What do you mean by that? Do you think it is wrong?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
0
  #8
Oct 7th, 2009
the implicite variable "this" is called even if it is omited:
so writing addMouseListener(this); issimilar to it's similar to this.addMouseListener(this);
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,714
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #9
Oct 7th, 2009
Originally Posted by moutanna View Post
the implicite variable "this" is called even if it is omited:
so writing addMouseListener(this); issimilar to it's similar to this.addMouseListener(this);
I didn't understood you correctly, I thought you said this was an error
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,020
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
0
  #10
Oct 7th, 2009
Originally Posted by javaAddict View Post
What do you mean by that? Do you think it is wrong?
No, there's nothing wrong with it all. It's definitely not ambiguous!
  1. addMouseListener(this);
calls the addMouseListener method on the current object - the thing called "this".
  1. this.addMouseListener(this);
just makes that fact explicit, but the first "this" is unnecessary.

People don't normally code the "this" in this.someMethod, but there are times when you may want to do that to draw a reader's attention to what you are doing.
Last edited by JamesCherrill; Oct 7th, 2009 at 8:36 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1192 | Replies: 19
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC