0

is there a command that returns the componente associated to a speciefied String name in Java Swing CardLayout? I have many panels in my CardLayuot which are at first empty, at runtime i have to "update" those panels to add buttons in it (the panels already have GridBagLayout), i would prefer NOT to make panels all over again and replace the old ones, as it would be slower to charge, and instead get those panels and add something. Thanks guys! (i checked this but there's nothing like what i need https://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html)

  • You mean get the card by name? – Frakcool Jun 27 '17 at 16:03
  • yes exactly what i need, is there a command like this? – Gabriel Logronio Jun 27 '17 at 18:55
  • The [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) shows how to do it – Frakcool Jun 27 '17 at 19:24
  • they only use add(panel, string), which just replces the old one, i would prefer to get the old and modify it – Gabriel Logronio Jun 27 '17 at 19:27
  • What is the layout (I mean provide ascii art / image and code) that you're trying to achieve? How many buttons (and how are they distributed among the UI) are you going to be adding? (And why?) – Frakcool Jun 27 '17 at 19:34
  • *"I have many panels in my CardLayuot which are at first empty"* Why do they start empty? One of the advantages of using card layout is that once the components are added, we can pack the GUI to the correct size and it will account for the tallest and widest cards. – Andrew Thompson Jun 29 '17 at 02:27

1 Answers1

1

Every Component, such as JPanel, has a name field, and you can set it with setName(). If your program already has a List<JPanel>, you can search the list and check getName(). If not, you can add your panels to a Map<String, JPanel> and get() the one you want by name.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42