944,200 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5512
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 6th, 2009
0

Dispose a JFrame using JPanel

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kathmartir is offline Offline
3 posts
since Oct 2009
Oct 6th, 2009
0
Re: Dispose a JFrame using JPanel
When I want to go from one frame to another I use:
Java Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 6th, 2009
0
Re: Dispose a JFrame using JPanel
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

Click to Expand / Collapse  Quote originally posted by kathmartir ...
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.
Reputation Points: 8
Solved Threads: 15
Light Poster
sincerelibran is offline Offline
41 posts
since Mar 2009
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
I use the setVisible method, still it won't work. . .try to look at the code below:
Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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, 46 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kathmartir is offline Offline
3 posts
since Oct 2009
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
this instruction is ambigous: addMouseListener(this); it's similar to this.addMouseListener(this);
Reputation Points: 16
Solved Threads: 57
Posting Whiz
moutanna is offline Offline
385 posts
since Oct 2009
Oct 7th, 2009
1
Re: Dispose a JFrame using JPanel
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
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
Click to Expand / Collapse  Quote originally posted by moutanna ...
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?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
the implicite variable "this" is called even if it is omited:
so writing addMouseListener(this); issimilar to it's similar to this.addMouseListener(this);
Reputation Points: 16
Solved Threads: 57
Posting Whiz
moutanna is offline Offline
385 posts
since Oct 2009
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
Click to Expand / Collapse  Quote originally posted by moutanna ...
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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 7th, 2009
0
Re: Dispose a JFrame using JPanel
Click to Expand / Collapse  Quote originally posted by javaAddict ...
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!
Java Syntax (Toggle Plain Text)
  1. addMouseListener(this);
calls the addMouseListener method on the current object - the thing called "this".
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1939
Solved Threads: 954
Posting Expert
JamesCherrill is online now Online
5,815 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: using if statement
Next Thread in Java Forum Timeline: Difficulties





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC