4

i'm using cardlayout in application and want to get the name of the current active card i tried many methods but with no mean, for example this method seems to be no longer exists

panel.getLayout().getActiveItem()

also i tried this but it doesn't work too

 Panel card = null;
      for (Component comp : cardPanel.getComponents()) {
           if (comp.isVisible()) {
                System.out.println(card.getName());

                    }
                 }

for example: the following stamts adds several panels to a card layout, i want to return 1,2,3,4 or 5 of the currently active card:

    cardPanel.add(firstP, "1");
    cardPanel.add(secondP, "2");
    cardPanel.add(thirdP, "3");
    cardPanel.add(fourthP, "4");
    cardPanel.add(fifthP, "5");

what's the possible ways to do that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165

2 Answers2

4

CardLayout does not expose its own mapping between of components and its keys (here 1, 2, 3, 4...), so using the layout itself will not reveal how the "cards" are hashed.

If you wish to use the getName method, remember that you must set this yourself first as the field is not set by default:

firstPanel.setName("1");
cardPanel.add(firstPanel, firstPanel.getName());

then, using your for loop, you will be able to get the current card String.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

there are two ways

code

(Disclaimer, Notice this is only code example with Thread.sleep(int), and works only in this form, there is designedly locked Event Dispatch Thread)

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class CardlayoutTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel pnlA, pnlB, pnlC;
    public CardLayout card = new CardLayout();

    public CardlayoutTest() {
        EventHandler eventHandle = new EventHandler();
        pnlA = new JPanel(new BorderLayout());
        pnlA.add(new JButton("A"), BorderLayout.CENTER);
        pnlA.putClientProperty("JPanel", "JPanel_a");
        pnlB = new JPanel(new BorderLayout());
        pnlB.add(new JButton("B"), BorderLayout.CENTER);
        pnlB.putClientProperty("JPanel", "JPanel_b");
        pnlC = new JPanel(new BorderLayout());
        pnlC.add(new JButton("C"), BorderLayout.CENTER);
        pnlC.putClientProperty("JPanel", "JPanel_c");
        pnlA.addAncestorListener(eventHandle);
        pnlA.addHierarchyListener(eventHandle);
        pnlB.addAncestorListener(eventHandle);
        pnlB.addHierarchyListener(eventHandle);
        pnlC.addAncestorListener(eventHandle);
        pnlC.addHierarchyListener(eventHandle);
        setLayout(card);
        add(pnlA, "A");
        add(pnlB, "B");//
        add(pnlC, "C");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    class EventHandler implements AncestorListener, HierarchyListener {

        @Override
        public void ancestorAdded(AncestorEvent event) {
            JComponent comp2 = event.getComponent();
            String str = (String) comp2.getClientProperty("JPanel");
            System.out.println("ancestorAdded " + str);
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
            JComponent comp2 = event.getComponent();
            String str = (String) comp2.getClientProperty("JPanel");
            System.out.println("ancestorMoved " + str);
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            JComponent comp2 = event.getComponent();
            String str = (String) comp2.getClientProperty("JPanel");
            System.out.println("ancestorRemoved " + str);
        }

        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            JComponent comp2 = (JComponent) e.getComponent();
            String str = (String) comp2.getClientProperty("JPanel");
            System.out.println("hierarchyChanged " + str);
        }
    }

    public static void main(String[] args) {
        CardlayoutTest t = new CardlayoutTest();
        t.setSize(300, 200);
        System.out.println("CardlayoutTest.main()------------------------ FIRST");
        t.card.show(t.getContentPane(), "A");
        t.setVisible(true);
        System.out.print("\n");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("CardlayoutTest.main()------------------------ SECOND");
        t.card.show(t.getContentPane(), "B");
        System.out.print("\n");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("CardlayoutTest.main()------------------------ THIRD");
        t.card.show(t.getContentPane(), "C");
        System.out.print("\n");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("CardlayoutTest.main()------------------------ SECOND");
        t.card.show(t.getContentPane(), "B");
        System.out.print("\n");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        System.out.println("CardlayoutTest.main()------------------------ FIRST");
        t.card.show(t.getContentPane(), "A");
        System.out.print("\n");
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319