Results 1 to 12 of 12
Thread: Close a frame from another class
- 03-27-2011, 05:38 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Close a frame from another class
An OO question.
Goal: i want to dispose the Frame or setvisibility to false to the Frame in class two (StartHandler)
Only i don't know how i can close the active frame. An instance won't work, it will be a new instance.
The code:
1. Method in class One - Button panel,
Java Code:class RestartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { } }
2. Method in class Two - StartHandler, starting two panels in one frame.
Java Code:public void startGame(String name, int levelId) { this.naam = name; this.levelId = levelId; this.playgroundId = 3; if(checkNaam(this.naam)) { Game game = new Game(this.name); game.setLevelActive(this.levelId); game.setPlaygroundActive(this.playgroundId); //maak panel rechts voor knoppen ButtonPanel panelEast = new ButtonPanel(); panelEast.setLayout(new GridLayout(4, 4)); panelEast.setFocusable(true); //maak panel links midden voor het speelveld PlaygroundPanel panelCenter = new PlaygroundPanel(game.getPlayground(), 30, 30, 21, 21, false); panelCenter.setFocusable(true); //maak parent panel JPanel parent = new JPanel(); parent.setLayout(new BorderLayout()); parent.add(panelEast, BorderLayout.EAST); parent.add(panelCenter, BorderLayout.CENTER); JFrame frame = new JFrame("Game: Sokoban - Level: " + game.level.getNaam() + " - Grameground: " + game.playground.getNaam() + " - Speler: " + spel.getPlayernaam()); frame.setSize(750, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.add(parent); } }
- 03-27-2011, 05:45 PM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
IS framename.dispose(); not working??
- 03-27-2011, 05:49 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Well its in two seperated classes and i most build the frame in a method with a few parameters.
So like i said with a new instance i'm not closing the form in question.
-
What you need is a reference to the visible JFrame in the class that needs to close it. How this is done depends on how your code is constructed but can be as simple as giving the class that needs to close the JFrame a public setter method that allows other objects to pass the JFrame reference into it.
- 03-27-2011, 06:21 PM #5
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Hi Fubarable,
I was testing with this.
So in class two -StartHandler, i do this:
public void setClose(JFrame jframe)
{
this.frame = frame;
this.frame.setVisibility(false);
}
But when i go back in the buttonPanel i need to call this method en now i need a reference i don't have. If you see the code.
Or do i need to make another class for this frame?
Kind regards,
André
-
It's hard for me to say based on what you have posted. I suggest that you create a very small version of your problem, code that compiles and runs for us without any image files or other outside dependencies, code that and shows your JFrame and also has code that should make it disappear, an SSCCE. This will take effort on your part, but will help us get you a helpful solution a lot sooner than having us guess at the true nature of your problem.
-
Also, do you even want to close a JFrame? Since the ActionListener is labeled "RestartButtonHandler" would it not be better to "reset" the GUI rather than get rid of a JFrame and show a new one?
- 03-27-2011, 07:01 PM #8
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Hi Furabele,
Well its a lot of code ;-).
And your right, this one handler is for the reset of it al and another one is to close the form and to start another form, this one i did mean.
I have changed it a bit and maybe this helps to see what i mean:
1. I got a frame Class
2. I want to run the closing method from this frame class, with a button of another button class.Java Code:public class GameForm extends JFrame{ private Spel spel; private JPanel parent; private GameForm frame; public GameForm (JPanel parent, Spel spel){ this.parent=parent; this.spel =spel; } public void creatingFrame(){ // bouw het frame waarin het spel moet draaien op JFrame frame = new JFrame("Spel: Sokoban - Level: " + spel.level.getNaam() + " - Speelveld: " + spel.playground.getNaam() + " - Speler: " + spel.getPlayernaam()); frame.setSize(750, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //boolean starten en stoppen van dit frame frame.setVisible(true); // voeg parent panel toe frame.add(parent); } public void closingFrame(){ frame.setVisible(false); } }
But the gameForm constructs form a third class. So the constructor needs his parameters, who i do not have in the button class.
Is there any other option to close this frame? See menuButtonHandler
Code of button class.
Java Code:package view; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JPanel; /** * @version 1.0 * @author Andre /Stefan * * ButtonPanel voor de rechterkant van het speelveld * * @param buttonGroup * @param restartButton * @param stopButton * @param hoofdMenuButton * */ public class ButtonPanel extends JPanel { private ButtonGroup buttonGroup; private JButton restartButton; private JButton hoofdMenuButton; public ButtonPanel() { // maak knoppen buttonGroup = new ButtonGroup(); restartButton = new JButton("Herstart") ; restartButton.addActionListener(new RestartButtonHandler()); hoofdMenuButton = new JButton("Hoofdmenu"); hoofdMenuButton.addActionListener( new HoofdMenuButtonHandler()); //voeg knoppen toe aan groep buttonGroup.add(restartButton); buttonGroup.add(hoofdMenuButton); //bepaal size van knoppen binnen de vertical Box Dimension buttonSize = new Dimension(100, 20); //Maak de vertical box Box buttonBox = Box.createVerticalBox(); //ken de size toe aan de knoppen restartButton.setMinimumSize(buttonSize); restartButton.setPreferredSize(buttonSize); restartButton.setMaximumSize(buttonSize); hoofdMenuButton.setMinimumSize(buttonSize); hoofdMenuButton.setPreferredSize(buttonSize); hoofdMenuButton.setMaximumSize(buttonSize); //Voeg knoppen toe aan vertical box buttonBox.add(restartButton); buttonBox.add(hoofdMenuButton); //Voeg vertical box toe aan panel add(buttonBox); } /* * @type constructor */ class RestartButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { } } /* * @type: constructor */ class menuButtonHandler implements ActionListener { /* * @type: function * @param: Actionevent */ public void actionPerformed(ActionEvent e) { GameForm stop = new GameForm() //gives error of missing contructor params stop.closingFrame();; } } }
-
In your ButtonPanel class, consider adding a GameForm field:
Java Code:public class ButtonPanel extends JPanel { private ButtonGroup buttonGroup; private JButton restartButton; private JButton hoofdMenuButton; private GameForm gameForm;
and give it a public setGameForm(GameForm gameForm) method:
Java Code:public void setGameForm(GameForm gameForm) { this.gameForm = gameForm; }
Then set the GameForm reference in the object of the ButtonPanelclass to refer to the actually visible GameForm object, and in your menuButtonHandler class (which should be capitalized by the way), use the referred object:
Java Code:class MenuButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (gameForm != null) { gameForm.closingFrame(); } } }
Note that if a new GameForm object becomes visible, then you are responsible for calling setGameForm again for your ButtonPanel class so that it always holds a reference to the currently visible GameForm object.
-
And again, if this doesn't help, then you should create and post an SSCCE (see link in signature below).
- 03-27-2011, 07:20 PM #11
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
I will, thanks
- 03-27-2011, 08:22 PM #12
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Similar Threads
-
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
How do I close a frame with a button?
By Psyclone in forum AWT / SwingReplies: 7Last Post: 02-19-2010, 10:43 PM -
how disable the display of close button on the frame
By kalanidhi in forum New To JavaReplies: 6Last Post: 11-19-2008, 09:51 AM -
close a frame..
By tajinvillage in forum New To JavaReplies: 5Last Post: 04-27-2008, 10:22 PM -
Frame close operation
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks