0

Can anyone tell me how to go about coding for navigation between multiple JPanel classes taking the event trigger from JButton from the objects (panels) themselves? I have read about CardLayout. The panel can be swapped from the events happening in the parent panel. What I want to achieve is on click of a button embedded in the panel, it should should disappear or a desired panel should be displayed. Can't seem to find a solution.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CodeThag
  • 63
  • 3
  • 7
  • 3
    *"Can anyone tell me.."* Sure we can, but [what have *you* tried?](http://www.whathaveyoutried.com/) I mean *besides* asking random strangers on the internet to do it for you. – Andrew Thompson Jun 05 '12 at 16:47
  • 2
    You are correct -- CardLayout can do this. Now all you have to do is try to write the code and see what happens. *Then* if you get stuck, please come on back and show us your code and tell us how or why it's not working. Don't try to *find* a solution, but instead try to *write* a solution. You will learn more from the effort, trust me. – Hovercraft Full Of Eels Jun 05 '12 at 17:12

2 Answers2

3

There is nothing about CardLayout that prevents switching cards from actions of children within the cards.

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

public class Testing extends JFrame {

    private JPanel cardHolder;
    private CardLayout cards;
    private static final String cardA = "A";
    private static final String cardB = "B";

    private class Switcher implements ActionListener{
        String card;        
        Switcher(String card) { this.card = card; }
        @Override
        public void actionPerformed(ActionEvent e) {
            cards.show(cardHolder, card);
        }
    }

    private void run() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel pa = new JPanel();
        JButton ba = new JButton("Switch to B");
        ba.addActionListener(new Switcher(cardB));
        pa.add(ba);
        pa.setBackground(Color.CYAN);

        JPanel pb = new JPanel();
        JButton bb = new JButton("Switch to A");
        bb.addActionListener(new Switcher(cardA));
        pb.add(bb);
        pb.setBackground(Color.MAGENTA);

        cardHolder = new JPanel();
        cards = new CardLayout();
        cardHolder.setLayout(cards);
        cardHolder.add(pa, cardA);
        cardHolder.add(pb, cardB);
        add(cardHolder);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    new testing().run();
                }
            });
        } catch (Exception ex) { }
    }
}
Gene
  • 46,253
  • 4
  • 58
  • 96
  • 1
    +1 but 1) never `extends JFrame / JDialog / JWindow` 2) use Java Naming Conventions correctly, then class name `testing` should be `Testing` – mKorbel Jun 05 '12 at 18:45
  • Gene, Thank alot works like a charm. My errors were trying to use panel.setVisible(), revalidate(), and repaint() methods to hide/show the panels. Sorry, I didn't post my codes I felt it won't do much. Thanks alot. I am new to StackOverFlow, is there anywhere I can +1 a solution. – CodeThag Jun 06 '12 at 08:57
  • *"is there anywhere I can +1 a solution."* Not until you have enough rep. points. Accepting an answer (like you did) is always a good way to indicate satisfaction with an answer. – Andrew Thompson Jun 06 '12 at 12:54
  • Thanks. I always keep a little class called testing in my current project as a place to do quick experiments. I use an all lower case name to allow my build script to strip it in the final build. – Gene Jun 06 '12 at 15:43
  • I'm not sure what you mean by `never extends JFrame`. The run() method extends `JFrame`, and I can't think of a way it would be clearer if it didn't do this. Cheers. – Gene Jun 06 '12 at 15:50
2

You can use JMenu with JMenuItems instead of to use the JButton for switching betweens Cards

mKorbel
  • 109,525
  • 20
  • 134
  • 319