Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CardLayout

 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy all,
how can I get the active (showing) Component when using a CardLayout?
Thanks
 
Stanley George
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple
first u add em. in case ur componets to be added are ,say JPanel
then..
panel.add (chatPanel, "one");
panel.add (whiteboardPanel, "two");
then u add action listeners & then depending on the action command..
cardLayout.show(panel, "one");
cardLayout.show(panel, "two");
enjoy
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helmut,
Ummm... you can't... but your program can keep track of this if it needs to. Can you give some code or at least a description of what you are trying to do?
Thanks,
-Nate
 
Stanley George
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi helmut
correct me if i am wrong.
cant u just have a globla variable :"String activeWindow"
which is updated by getActionCommand()
in that way activeWindow will contain the name of the window
which is currently showing...
 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Pruett:
Helmut,
Ummm... you can't... but your program can keep track of this if it needs to. Can you give some code or at least a description of what you are trying to do?
Thanks,
-Nate

I do it until now by keeping track within my Code (Hashtable with String to Component, Buttons to select within CardLayout, ActionListener and get() on Hashtable). I thought it would be nice if there is a method like getComponentShowing() in the CardLayout.
Thanks for answering
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree it would be nice to have, and I spent some time myself looking while working of a previous project. In practice, Sun seem to have never been very keen on CardLayout, so it has never had much support or development.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic