| | |
Dispose a JFrame using JPanel
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
0
#2 Oct 6th, 2009
When I want to go from one frame to another I use:
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
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2009
Posts: 16
Reputation:
Solved Threads: 9
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
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
•
•
•
•
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.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#4 Oct 7th, 2009
I use the setVisible method, still it won't work. . .try to look at the code below:
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. . .
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.util.*; public class MainPage extends JFrame { private MainPagePanel mpp; public MainPage() { super("Test"); setSize(180,100); setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); mpp = new MainPagePanel(); Container pane = getContentPane(); pane.add(mpp); setContentPane(mpp); setResizable(false); setVisible(true); } public static void main(String[] args) { MainPage mp = new MainPage(); } }
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; public class MainPagePanel extends JPanel { ImageIcon image; JLabel display; public MainPagePanel() { Toolkit kit = Toolkit.getDefaultToolkit(); image = new ImageIcon("menu1.jpg"); display = new MapLabel(image, "Display"); display.setBounds(10, 20, image.getIconWidth(), image.getIconHeight()); setPreferredSize(new Dimension(457, 540)); setLayout(null); add(display); } class MapLabel extends JLabel implements MouseListener { Icon icon; String name; MainPage mp; // Declaring MainPage mp = new MainPage(); here will result to error public MapLabel(Icon icon, String name) { this.icon = icon; this.name = name; setIcon(icon); setToolTipText(name); addMouseListener(this); } public void mouseClicked(MouseEvent e) { String image = name; if(image.equals("Display")) { // How can I close the MainPage program if I click the display image label before PageOne will appear? // setVisibility won't work. PageOne po = new PageOne(); mp.setVisible(false); // disposing PageOne mp.dispose(); //disposing PageOne mp = new MainPage(); } } public void mouseEntered(MouseEvent e) { setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } public void mouseExited(MouseEvent e) { setCursor(null); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } }
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. . .
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
But in order to call this:
MainPagePanel class
And now to pass the MainPage that is opened as paramater
MainPage class
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
It is better to call dispose on the PageOne:
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
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 anotherBut 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)
class PageOne { MainPage mp; public PageOne(MainPage mp) { this.mp = mp; } .... ... // in some method: { this.setVisble(false); //hiding PageOne mp.setVisible(true); } }
It is better to call dispose on the PageOne:
Java Syntax (Toggle Plain Text)
mp.setVisible(true); 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
•
•
Join Date: Apr 2008
Posts: 1,020
Reputation:
Solved Threads: 151
0
#10 Oct 7th, 2009
No, there's nothing wrong with it all. It's definitely not ambiguous!
calls the addMouseListener method on the current object - the thing called "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.
Java Syntax (Toggle Plain Text)
addMouseListener(this);
Java Syntax (Toggle Plain Text)
this.addMouseListener(this);
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.
![]() |
Similar Threads
- Exiting 1 out of 2 JFrames using a JButton? (Java)
- Dynamic JPanel additions to a JFrame/JPanel (Java)
- Dispose/hide the JFrame on a click-event (Java)
- program code about log in information using java JFrame (Java)
- Questions about Applet and JFrame (Java)
- JFrame , JPanel and the rest... (Java)
- Failed to load image into Jpanel (Java)
Other Threads in the Java Forum
- Previous Thread: using if statement
- Next Thread: Difficulties
Views: 1192 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat chooser class classes client code compile compiler component converter database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort sql string swing system test threads time transfer tree variablebinding windows xor






