Results 1 to 12 of 12
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default 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);
    
            }
        }

  2. #2
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    IS framename.dispose(); not working??

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aborgeld View Post
    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.

    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.

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    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é

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    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

    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);
            }
    
    }
    2. I want to run the closing method from this frame class, with a button of another button class.
    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();;
                
            }
        }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  11. #11
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    I will, thanks

  12. #12
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    It didn't work, but allthrought thanks for brainstorming with me..
    I got a Nullpointer. But i changed the software, i put the frame and buttons in one view, named GamePanel. And now i can controle the Frame because its in the same class. And the design is still ok this way.

Similar Threads

  1. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  2. How do I close a frame with a button?
    By Psyclone in forum AWT / Swing
    Replies: 7
    Last Post: 02-19-2010, 10:43 PM
  3. how disable the display of close button on the frame
    By kalanidhi in forum New To Java
    Replies: 6
    Last Post: 11-19-2008, 09:51 AM
  4. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 10:22 PM
  5. Frame close operation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •