Remove Jpanel from view

Asked

Viewed 898 times

2

I would like to know how to remove one JPanel that is inside a JFrame.

I don’t want to use the information or anything, I just want it to close.

Use setVisible(false) makes the panel invisible, I don’t want it, I just want it to close.

I looked on the internet but I also did not get much success, how to do this?

Jframe code:

    public class MenuAultima extends JFrame { 
    public MenuAultima() {
        add(new Fundo());
        setTitle("A última esperança");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,625);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    public static void main (String[]args){
        new MenuAultima();
    }
}

Code of the Jpanel:

    public class Fundo extends JPanel  implements MouseListener{
    private Image fundo; 
    private Image novojogo;
    private Image comojogar;
    private Image  sair;
    public Fundo() {
        setFocusable(true);
        addMouseListener(this);
        ImageIcon Menu = new ImageIcon ("Imagens-menu\\menu.fw.png");
        fundo = Menu.getImage();
        ImageIcon botao = new ImageIcon ("Imagens-menu\\novojogo.fw.png");
        novojogo = botao.getImage();
        ImageIcon botaodois = new ImageIcon ("Imagens-menu\\comojogar.fw.png");
        comojogar = botaodois.getImage();
        ImageIcon botaotres = new ImageIcon ("Imagens-menu\\sair.fw.png");
        sair = botaotres.getImage();
    }
    public void paint (Graphics g){
        Graphics2D graficos = (Graphics2D) g;
        graficos.drawImage(fundo, 0, 0, null);
        graficos.drawImage(novojogo, 511, 346, null);
        graficos.drawImage(comojogar, 498, 410, null);
        graficos.drawImage(sair, 561 , 469 , null);
    }
    public Rectangle getBotaoComoJogar(){
        return new Rectangle(498, 410, comojogar.getWidth(null), comojogar.getHeight(null));
    }
    public String coords(MouseEvent e){
        return e.getX() + ", " + e.getY();
    }
    public void mouseClicked(MouseEvent e){
        Rectangle BotaoComoJogar = getBotaoComoJogar();
        if (BotaoComoJogar.contains(getMousePosition())){

            //Coisas acontecem aqui

        }
    }
    public void mousePressed(MouseEvent e){

    }
    public void mouseReleased(MouseEvent e){

    }
    public void mouseEntered(MouseEvent e){

    }
    public void mouseExited(MouseEvent e){

    }
}

Where there is the comment I would like to put an action where the Jpanel Background was closed and another Jpanel for example, how to play, was opened.

  • Add your jpanel code, setvisible() already meets what you want, there is no other way to hide a component, but the space it occupies will not disappear.

  • @Diegof This makes it invisible but does not close. It will bring problems in my code in the future. There is really no other way?

  • Isn’t there a . Dispose() function? Have you tried searching for something more targeted to it? Maybe it answers what you need.

  • @Leonardocoelho Ispose is for top-level containers such as jframe, jpanel does not have Ispose.

  • Gregory you can remove the component by using seuFrame.remove(seuJPanel). Other than that, I don’t see any less laborious way to do what you want.

  • @Diegof Yes, I was trying to use this, but like this: There’s a button inside my Jpanel that should make it close and open another Jpanel, you know?

  • It would be interesting to add the code to the question and edit it, explaining better how your application works, what you want to do and what you can’t. It makes it even easier to answer.

  • Edited. I explained exactly what I want to happen there

Show 3 more comments

1 answer

2


As his JPanel is a class to part, you will need to recover the JFrame on it, so you can add the new panel and remove the current one. I do not consider this to be good practice, as I understand that we are delegating functions to the JPanel which should be the responsibility of the JFrame.

Anyway, adapting to your code would look like this:

public void mouseClicked(MouseEvent e){
    Rectangle BotaoComoJogar = getBotaoComoJogar();
    if (BotaoComoJogar.contains(getMousePosition())){
        JFrame janelaPrincipal = (JFrame) SwingUtilities.getWindowAncestor(this);
        janelaPrincipal.remove(this);
        janelaPrincipal.add(new OutroJPanel());
        janelaPrincipal.revalidade();
    }
}

The method SwingUtilities.getWindowAncestor() will retrieve the JFrame where you inserted the panel(the panel should be passed as argument, so I used the this). Note that if the panel is added to another, this line will give a cast error, as can be seen in the documentation, this method returns the window only if the component passed has been added to one. If it is inside another panel, the method returns null.

The method revalidade() is who will notify the window that there have been changes and that it should be redesigned.

  • 1

    I know it’s not good practice, but I’m still learning Java.. Learn from mistakes! Thank you! I will argue further with my teacher, it was only this question that he could not help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.