Duvida com Swing - Fechar um Jpanel

Estou com dúvida para executar um comando de fechar o Jpanel e retornar ao Jframe com opção de abrir outro Jpanel, pois do jeito que está não está limpando a tela. o programa abre o jpanel mas ele fica fixo.

Programa com o menu

package revisao;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class painelCadastro extends javax.swing.JFrame {
	private JMenuBar jMenuBar1;
	private JMenu jMenu5;
	private JPanel jPanel1;
	private JMenuItem jMenuItem6;
	private JMenuItem jMenuItem5;
	private JMenuItem jMenuItem4;
	private JMenu jMenu2;
	private JMenuItem jMenuItem3;
	private JMenuItem jMenuItem2;
	private JMenuItem jMenuItem1;
	private JMenu jMenu1;
	//editado manualmente para chamar as telas
	private NewJPanelEstagiario JPanelEstagiario = new NewJPanelEstagiario();
	private NewJPanelFuncionario JPanelFuncionario = new NewJPanelFuncionario();
	private NewJPanelDiasTrabalhados JPanelDiasTrabalhados = new NewJPanelDiasTrabalhados();

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				painelCadastro inst = new painelCadastro();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public painelCadastro() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
			}
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu1 = new JMenu();
					jMenuBar1.add(jMenu1);
					jMenu1.setText("Cadastrar");
					{
						jMenuItem1 = new JMenuItem();
						jMenu1.add(jMenuItem1);
						jMenuItem1.setText("Funcionários");
						jMenuItem1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jMenuItem1ActionPerformed(evt);
							}
						});
					}
					{
						jMenuItem2 = new JMenuItem();
						jMenu1.add(jMenuItem2);
						jMenuItem2.setText("Estagiário");
						jMenuItem2.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jMenuItem2ActionPerformed(evt);
							}
						});
					}
					{
						jMenuItem6 = new JMenuItem();
						jMenu1.add(jMenuItem6);
						jMenuItem6.setText("Horas Trabalhadas");
						jMenuItem6.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jMenuItem6ActionPerformed(evt);
							}
						});
					}
					{
						jMenuItem3 = new JMenuItem();
						jMenu1.add(jMenuItem3);
						jMenuItem3.setText("Sair");
						jMenuItem3.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								jMenuItem3ActionPerformed(evt);
							}
						});
					}
				}
				{
					jMenu5 = new JMenu();
					jMenuBar1.add(jMenu5);
					jMenu5.setText("Calcular");
					{
						jMenuItem5 = new JMenuItem();
						jMenu5.add(jMenuItem5);
						jMenuItem5.setText("Salário");
					}
				}
				{
					jMenu2 = new JMenu();
					jMenuBar1.add(jMenu2);
					jMenu2.setText("Ajuda");
					{
						jMenuItem4 = new JMenuItem();
						jMenu2.add(jMenuItem4);
						jMenuItem4.setText("Sobre");
					}
				}
			}
			pack();
			this.setSize(515, 390);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jMenuItem3ActionPerformed(ActionEvent evt) {
		//botão sair no menu Cadastros
		System.exit(0);
	}
	
	private void jMenuItem2ActionPerformed(ActionEvent evt) {
		//botão Estagiário no menu Cadastros
		JPanelEstagiario.setVisible(true);
		this.getContentPane().remove(jPanel1);
		this.getContentPane().add(JPanelEstagiario);
		JPanelEstagiario.setPreferredSize(new java.awt.Dimension(507, 340));
		pack();
	}
	
	private void jMenuItem1ActionPerformed(ActionEvent evt) {
		//botão Funcionário no menu Cadastros
		JPanelFuncionario.setVisible(true);
		this.getContentPane().remove(jPanel1);
		this.getContentPane().add(JPanelFuncionario);
		JPanelFuncionario.setPreferredSize(new java.awt.Dimension(507, 340));
		pack();
	}
	
	private void jMenuItem6ActionPerformed(ActionEvent evt) {
		//botão Dias no menu Cadastros
		JPanelDiasTrabalhados.setVisible(true);
		this.getContentPane().remove(jPanel1);
		this.getContentPane().add(JPanelDiasTrabalhados);
		JPanelDiasTrabalhados.setPreferredSize(new java.awt.Dimension(507, 340));
		pack();
	}

}

Programa com o Jpanel com o botão abandonar

package revisao;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import sun.java2d.Disposer;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJPanelEstagiario extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JTextField jTextField1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JButton jButton1;
	private JLabel jLabel6;
	private JButton jButton3;
	private JButton jButton2;
	private JTextField jTextField5;
	private JTextField jTextField4;
	private JTextField jTextField3;
	private JTextField jTextField2;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new NewJPanelEstagiario());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public NewJPanelEstagiario() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Nome");
				jLabel1.setBounds(22, 31, 190, 14);
			}
			{
				jTextField1 = new JTextField();
				this.add(jTextField1);
				jTextField1.setBounds(22, 47, 333, 21);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Data de Nascimento");
				jLabel2.setBounds(22, 78, 169, 14);
			}
			{
				jTextField2 = new JTextField();
				this.add(jTextField2);
				jTextField2.setBounds(22, 95, 94, 21);
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Qual a área do estágio?");
				jLabel3.setBounds(22, 124, 283, 14);
			}
			{
				jTextField3 = new JTextField();
				this.add(jTextField3);
				jTextField3.setBounds(22, 140, 249, 21);
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setText("Instituição de ensino?");
				jLabel4.setBounds(22, 173, 179, 14);
			}
			{
				jTextField4 = new JTextField();
				this.add(jTextField4);
				jTextField4.setBounds(22, 190, 249, 21);
			}
			{
				jLabel5 = new JLabel();
				this.add(jLabel5);
				jLabel5.setText("Qual a quantidade de horas que ira trabalhar?");
				jLabel5.setBounds(22, 220, 304, 14);
			}
			{
				jTextField5 = new JTextField();
				this.add(jTextField5);
				jTextField5.setBounds(22, 237, 94, 21);
			}
			{
				jButton2 = new JButton();
				this.add(jButton2);
				jButton2.setText("Limpar");
				jButton2.setBounds(136, 267, 98, 21);
			}
			{
				jButton3 = new JButton();
				this.add(jButton3);
				jButton3.setText("Abandonar");
				jButton3.setBounds(253, 267, 102, 21);
				jButton3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButton3ActionPerformed(evt);
					}
				});
			}
			{
				this.add(jLabel6);
				jLabel6.setText("Cadastro de Estagiário");
				jLabel6.setBounds(22, 11, 291, 14);
				jLabel6.setFont(new java.awt.Font("Tahoma",1,14));
				jLabel6.setForeground(new java.awt.Color(0,0,255));
			}
			{
				jLabel6 = new JLabel();
				jButton1 = new JButton();
				this.add(jButton1);
				jButton1.setText("Gravar");
				jButton1.setBounds(28, 267, 88, 21);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButton3ActionPerformed(ActionEvent evt) {
		//botão abandonar no NewJPanelEstagiario
		
	}

}

Não consegui rodar seu código aqui, estão faltando várias Classes. Fiquei meio confuso para entender a sua pergunta…

Você quer fazer um determinado JPanel desaperecer e aparecer um outro JPanel? Como em uma mudança de tela?

Tipo9… mudar de uma tela de cadastro para uma tela de consulta?

Para fazer um painel desaparecer é só dar um setVisible(false); nele. Ou removê-lo do content panel que está inserido e inserir o novo painel em seu lugar.

No site da sun* tem bons tutoriais de swing, no roseindia** tbm, e, lógico, aqui no GUJ.

Caso não tenha problema em mudar um pouco seu layout, você pode dar uma olhada em JTabbedPanel, que é aquele menu com abas. Pode lhe ser útil.

Explique melhor sua dúvida aí

É o seguinte:

Você fez isso para ele aparecer:

[code]JPanelEstagiario.setVisible(true);
this.getContentPane().remove(jPanel1);
this.getContentPane().add(JPanelEstagiario); [/cod
E quando fechar tem que fazer o inverso, tem que voltar o JPanel1 no JFrame,
só que você está em outro painel.
Para fazer isso você terá que enviar a instância do JPanel1 para os
outros painéis para eles voltarem com ele no JFrame quando clicado no botão fechar, isto dá um certo trabalho desnecessário em seu caso.

Outra forma é não usar:

this.getContentPane().remove(jPanel1); this.getContentPane().add(JPanelEstagiario);
E trocar os JPanels para extenderem JDialog ou JFrame.

Mas o melhor mesmo é deixar sempre os botões de troca de JPanels disponíveis, usando uma barra de ferramentas, ou se não uma barra com abas.

O JToolBar é bem simples e fica bem legal, veja uma criada com imagens:

ImageIcon image1 = new ImageIcon("button1.gif"); JButton button1 = new JButton(image1); ImageIcon image2 = new ImageIcon("button2.gif"); JButton button2 = new JButton(image2); ImageIcon image3 = new ImageIcon("button3.gif"); JButton button3 = new JButton(image3); JToolBar bar = new JToolBar(); bar.add(button1); bar.add(button2); bar.add(button3);

[quote=cassioso]Não consegui rodar seu código aqui, estão faltando várias Classes. Fiquei meio confuso para entender a sua pergunta…

OP1->Você quer fazer um determinado JPanel desaperecer e aparecer um outro JPanel? Como em uma mudança de tela?

OP2->Tipo… mudar de uma tela de cadastro para uma tela de consulta?

Para fazer um painel desaparecer é só dar um setVisible(false); nele. Ou removê-lo do content panel que está inserido e inserir o novo painel em seu lugar.

No site da sun* tem bons tutoriais de swing, no roseindia** tbm, e, lógico, aqui no GUJ.

Caso não tenha problema em mudar um pouco seu layout, você pode dar uma olhada em JTabbedPanel, que é aquele menu com abas. Pode lhe ser útil.

Explique melhor sua dúvida aí[/quote]

//-----------------------------------------------------------------------------------------

Minha dúvida é exatamente esta Mudança de Tela, OP1.

Obrigado

Olá,

Para desaparecer algo.dispose() e para aparecer algo.setvisible(true);

Espero ter ajudado …

Generosamente,

Frid

Eu tambem estava com a mesma dúvida.
O negócio é o seguinte, você abre um JPanel dentro de um JFrame e quando você faz isso você não consegue mais acessas o JFrame, pois agora você esta no JPanel, então você precisa fechar o JPanel por um botão no próprio JPanel.
Alguem tem alguma idéia ?
não existe o dispose() no JPanel

Você precisa do CardLayout:
http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

Ele gerencia as trocas de painel para você.

[quote=fringos bringos]Eu tambem estava com a mesma dúvida.
O negócio é o seguinte, você abre um JPanel dentro de um JFrame e quando você faz isso você não consegue mais acessas o JFrame, pois agora você esta no JPanel, então você precisa fechar o JPanel por um botão no próprio JPanel.
Alguem tem alguma idéia ?
não existe o dispose() no JPanel[/quote]

Passe o JFrame no construtor do JPanel:

[code]
public SeuPainel extends JPanel
{
private JFrame owner;
public SeuPainel(JFrame owner) {
super();
this.owner = owner;
}


}[/code]

Assim, você pode fechar o seu JFrame chamando owner.dispose().