layout - How to get the top card in Java's CardLayout -
Is it possible to get top card in Java's CardLayout? I have tried to check the visibility for visibility through each component () but it seems that they are all "visible".
Edit: "Top Card" I mean that currently the "Top" is being displayed, not the first or the last card. Also, I do not know that it helps, but I'm looking for a JPN (or its sub-class)
Edit: code snippet
(Component Components: GetComponents ()) {if component (component as; JPanel & amp; amp; amp; amp; amp;;; View)) [currentPanel = (JPanel) component; Println (currentPanel.getClass () getName ().); }} The above code always prints the name of each component class, whether or not it is a visual card.
Edit : I am using it as part of school work. I am not trying to get a freebie here, assignment around this layout Does not rotate. It seems to be the most convenient layout to change between panels. My teacher has specified that there should be no third party code in the project. I have seen the implementation with the link of the first camicator but I can not use it. I can apply it equally and maybe give a reference in this document, but I can not just download it and use it. Edit : The reason I'm trying to get the top card is because I have the toolbar with the "Add" button. One for each of my two potential things Instead of adding the buttons, I wanted to know which panel is currently being viewed, seeing this should just add. If there is a more appropriate way to do this, please let me know. Edit : Thanks to everyone for helping. I came to know what was the problem, I think it was my mistake because I did not provide enough details. My two cards are JScrollPane s and I also need to know whether I was looking at one of those panels. I did not check for the isisible () on the scroll panel, I was seeing that it was always visible, it was a scroll panel which required visibility to verify me. public JPanel getCurrentPanel () {JPanel currentPanel = null; (Component component: getComponents ()) {if (component.isVisible ()) {if (component examples of JPanel) currentPanel = (JPanel) component; Else if (component example JScrollPane) currentPanel = (JPanel) ((JScrollPane component) .ViewViewport () GetComponent (0); }} Current Return Pantal; }
If you modify the code snippet, you can tell it easily The card is visible in my test frame, when the button is pushed, it prints the name of the current view card before going to the next card. Modifying your code, it is important to keep in mind that you have to call getComponents () on the component that is actually cardless, in this way, only one of my children is showing the same I guess in my code. I am adding your zippan with the card layout to the matching callmans with the camera.
class testframe appraises ActionListener {public TestFrame () {cards = new JPanel (new cardlayout ()); Card_list = new zpnell [5]; For (int i = 0; i & lt; card_list.length; i ++) {string text = "card" + i; JPNL Card = Create card (text); Card_list [i] = Cards; Cards.add (card, text); } Add (card); } Public JPanel getCurrentCard () {JPanel card = zero; (Component comp: cards.getComponents ()) {if (comp.isVisible () == true) {card = (JPanel) comp; System.out.println (card.getName ()); }} System.out.println (); Return card; } PublicExchange Evt {JPanel card = getCurrentCard (); CardLayout cl = (card.outLayout ()); Cl.next (card); } Public Zenith Building Cards (String Lessons) {Jpnell Card = New Zpn (); Card.setName (text); Jebton BTN = new pocket (text); Btn.addActionListener (this); Card.add (btn); Return card; } JPN card; Jepnell [] card_list; } In the Action-proportioned () method, I have printed the name of the card, but you are currently referring to the card that appears.
Comments
Post a Comment