2

i made this code to navigate trough panel1 and panel2 with buttons.

(button1 and button2) but when i run my code the frame stays empty.

Can somebody explain me what i'm doing wrong and how i can accomplish

toggling between panel1 and panel2 in this way? Starting with panel1 first

Code:

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

public class togglepanel {

    public static void main(String[] args) {

        final JFrame frame = new JFrame();
        final JPanel panel1 = new JPanel();
        final JPanel panel2 = new JPanel();
        JButton button1 = new JButton("previous frame!");
        JButton button2 = new JButton("next frame");

        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setSize(600, 400);
        frame.add(panel1);
        frame.add(panel2);

        panel1.add(button2);
        panel1.setVisible(true);

        panel2.add(button1);
        panel2.setVisible(false);

        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                panel1.setVisible(true);
                panel2.setVisible(false);

            }
        });


        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                panel1.setVisible(false);
                panel2.setVisible(true);
            }
        });
    }
}

thanks in advance

1
  • 1
    +1 for sscce. Reformatted code; please revert if incorrect.
    – trashgod
    Apr 8, 2011 at 20:04

2 Answers 2

4

Use a layout manager.

frame.setLayout(new FlowLayout());

3
  • wow it works! it's that simple? haha what did this add exactly to make it more of a learning experience?
    – Opoe
    Apr 8, 2011 at 19:53
  • The layout manager handles setting the positions and the sizes of all of the components on the screen. If you don't use a layout manager you have to do all positioning and sizing yourself. Here is a good explanation of layout managers link
    – Eric Milas
    Apr 8, 2011 at 19:57
  • 2
    Use a different layout manager--JFrame's content pane uses BorderLayout by default. The add() calls both default to CENTER, and only one component can be there, so the second overwrites the first. Apr 8, 2011 at 20:13
4

Another useful way to do this, and I think better is to use a CardLayout and to add both JPanels to a container that uses this CardLayout. You can then easily swap views by calling the CardLayout methods.

e.g.,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TogglePanel {
   public static void main(String[] args) {
      final CardLayout cardlayout = new CardLayout();
      final JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      final Container contentPane = frame.getContentPane();
      contentPane.setLayout(cardlayout);

      final JPanel panel1 = new JPanel();
      final JPanel panel2 = new JPanel();
      JButton button1 = new JButton("previous frame!");
      JButton button2 = new JButton("next frame");
      contentPane.setPreferredSize(new Dimension(600, 400));
      contentPane.add(panel1, "Panel 1");
      contentPane.add(panel2, "Panel 2");
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setResizable(true);
      frame.setVisible(true);

      panel1.add(button2);
      panel2.add(button1);

      ActionListener btnListener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardlayout.next(contentPane);
         }
      };

      button1.addActionListener(btnListener);
      button2.addActionListener(btnListener);

   }
}
2
  • oh yes i also considered CardLayout, can you give some benefits from CardLayout that the other method doesn't have? :)
    – Opoe
    Apr 8, 2011 at 20:14
  • 2
    The CardLayout will make sure that things are sized OK, that the preferredSize of the container will be the preferredSize of the largest card-component. The CardLayout takes care of revalidating and repainting things, so it makes it more difficult to screw things up. It also allows both sequential changing of cards (via the next() and previous() method) and random access changes of cards (by using the String constants that you use to add the cards to the container). Apr 8, 2011 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.