Toggle Jpanels within a single Jframe

Asked

Viewed 12,003 times

2

In a Java code, I have a Jframe and within this Jframe I have a contentPane (Jpanel). I have several screens in Jpanel format, I want to exchange these Jpanels by clicking a button that is attached inside each Jpanel. By clicking this button, the contentPane will be modified using and displaying another Jpanel screen.

  • 3

    Hello user4611, welcome to Stack Overflow. So that we can help you more objectively, please update your question with a brief, self-contained code example. Make it clear what you’ve tried and where you’re having problems; for example, in your case, a code with the JFrame and the event that should change the contentPane instantiating JPanel outsiders. Not knowing what you’ve done and where your difficulties are makes it very difficult to help you, as well as indirectly help other users who might have the same problem as you.

  • You could post your code JFrame? So I can create a ActionListener that does what you asked, for you to use on your Button?

3 answers

3


Below your getContentPane() place an element of the type JPanel and define its type to CardLayout, as in the figure below:

cards

Your code looks something like this:

private void initialize() {
    frmPrinc = new JFrame(); //variável de instancia

    ... //set size, bounds, title, etc

    cards = new JPanel(new CardLayout()); //variável de instancia
    frmPrinc.getContentPane().add(cards, BorderLayout.CENTER);
}

Create a JPanel for each screen you want, and place them inside the cards. In my case, I preferred to create several methods to create each screen, but you can also do it in a right way but different from mine. My code went like this:

private void telaInicial() {
    telaInicial = new JPanel(); //variável de instancia
    telaInicial.setLayout(new BorderLayout());
    cards.add(telaInicial, "inicial"); //"inicial" é a chave a ser usada para chamar o objeto "telaInicial"

    ... //adiciona todos os elementos necessários no JPanel telaInicial
}

The method add is overloaded, and you have several options to use it, I chose to use a String to reference the object telaInicial, but you could also use a whole.

Behold: Container (Java Platform SE 7)

Create as many screens as you want, to switch from one screen to another do so:

CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "inicial"); //mudará para a tela inicial

1

Follow an example:

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

public class SeuFrame extends JFrame {

  private JPanel panel1;
  private JPanel panel2;
  private JButton botao1;
  private JButton botao2;

  public SeuFrame() {
    panel1 = new JPanel();
    panel2 = new JPanel();

    botao1 = new JButton( "Trocar para panel2" );
    botao1.setPreferredSize( new Dimension( 200, 30 ) );

    botao2 = new JButton( "Trocar para panel1" );
    botao2.setPreferredSize( new Dimension( 200, 30 ) );

    panel1.add( botao1 );
    panel2.add( botao2 );

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        JPanel panelTrocar = null;
        if ( e.getSource() == botao1 ) {
          panelTrocar = panel2;
        }
        else {
          panelTrocar = panel1;
        }
        getContentPane().removeAll();
        getContentPane().add( panelTrocar );
        revalidate();
        repaint();
      }
    };

    botao1.addActionListener( actionListener );
    botao2.addActionListener( actionListener );

    getContentPane().add( panel1 );
  }
}

0

Browser other questions tagged

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