CardLayouts: How can I find out which card is visible?

I was looking through the documentation for Swing CardLayout , and there seems to be no way to determine which card is currently showing, baked in to the class. But should there be a way to ask the layout which map it is currently showing, right?

Due to the limitations of the project, I cannot just expand it and add this functionality to the subclass, so if there is no such function, it means that I am stuck in tracking the state of the component external to the component (yuck!), Or there is some kind of is it another option, somewhere deep in Swing?

+6
source share
4 answers

According to How to use CardLayout ,

Conceptually, each component that CardLayout controls is like a playing card or a trading card in a stack where only the top card is visible at any time. You can select a card that is displayed in any of the following ways:

  • Having asked either the first or the last card in the order, he was added to the container
  • Turning the deck back or forward
  • By specifying a card with a specific name

Try

Component visibleComponent = foo.getComponent(0); 

where foo is an instance of JPanel using CardLayout .

Link:

+5
source

There seems to be no direct way to find out which card is active. I guess this is a design decision, not a mistake. You are responsible for tracking the active card.

However, I do not quite understand why you need to track this. Swing is an event, and you pretty much know which card is active when you receive an event.

+3
source

The basic tutorial describes how to switch betweens cards in CardLayout , many examples about CardLayout are better here or here

from the link I posted here How to get top card in CardLayout Java

or better way

 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.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; public CardLayout card = new CardLayout(); public CardlayoutTest() { EventHandler eventHandle = new EventHandler(); JPanel pnlA = new JPanel(new BorderLayout()); pnlA.add(new JButton("A"), BorderLayout.CENTER); JPanel pnlB = new JPanel(new BorderLayout()); pnlB.add(new JButton("B"), BorderLayout.CENTER); pnlA.addAncestorListener(eventHandle); pnlA.addHierarchyListener(eventHandle); pnlB.addAncestorListener(eventHandle); pnlB.addHierarchyListener(eventHandle); setLayout(card); add(pnlA, "A"); add(pnlB, "B"); setDefaultCloseOperation(EXIT_ON_CLOSE); } class EventHandler implements AncestorListener, HierarchyListener { @Override public void ancestorAdded(AncestorEvent event) { System.out.println("CardlayoutTest.EventHandler.ancestorAdded()"); } @Override public void ancestorMoved(AncestorEvent event) { System.out.println("CardlayoutTest.EventHandler.ancestorMoved()"); } @Override public void ancestorRemoved(AncestorEvent event) { System.out.println("CardlayoutTest.EventHandler.ancestorRemoved()"); } @Override public void hierarchyChanged(HierarchyEvent e) { System.out.println("CardlayoutTest.EventHandler.hierarchyChanged()"); } } public static void main(String[] args) { CardlayoutTest t = new CardlayoutTest(); t.setSize(500, 500); 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(), "B"); System.out.print("\n"); } } 
+2
source

The selected answer here gives the first component added to CardLayout, and I don’t think the user is asking. There is an easy way to find the visible panel by iterating over CardLayout components with:

 for(Component comp : cardPanel.getComponents()) { if (comp.isVisible()) { return (JPanel)comp; } } 

Putting this in a method returning a component

 Component getVisibleCard() { ... } 

Allows you to get the component that is currently displayed.

0
source

Source: https://habr.com/ru/post/895144/


All Articles