Card Layout
Objective #1: What is a Card Layout.
- A Card Layout is the layout that allows you to switch between different panels. For an easily identifiable example, a CardLayout is like today's internet browsers in that each tab would be a different Card or panel.
- FROM AUTHOR OF THIS PAGE DYLAN HOGUE(This type of layout is very useful if you have panels that have alot of info on them. by using these you can populate all of your data on your panels all during start up of your application, you can then just tell the program which panel to show so your program isn't bogged down with repopulating your data.)
- In your Card Layout you have multiple Panel objects. with this layout each Panel can have thier own layout, and they do not have to all be the same type of layout.*Idea from Dylan: This has in no way been tested but if my theory is correct then you could use a cardLayout to create animation without the flickering or any picture trails by switching back and forth between two similar panels and changing the one that is in the back before bringing it to the front*
Objective #2: How to impliment a Card Layout
- First you must start by adding the following import statements: import java.awt.CardLayout; AND import java.awt.Panel;
- Next you must initilize your cardLayout object using the following line: cardLayout = new CardLayout();
- Then you must initilize your different Panels which will be contained in your cardLayout (say we want 3 different panels). You would do this by using the code: Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel();
- You then would set up each panel seperatly(can't really give you any ideas here, it all depends on what you want. I can tell you that a panel is set up in the same way that an applet/JFrame is.)
Objective #3: Adding your Panels to your CardLayout
- First thing you want to do here is set your application up by using the code: this.setLayout(cardLayout) *cardLayout is the CardLayout() that you declared earlier*
- now you just simply add each Panel by calling: this.add(panel1, "panel1"); this.add(panel2, "panel2"); this.add(panel3, "panel3"); *Note the use of the add(component, objectconstraints) method*
Objective #4: Handling events from a custom class that extends panel
- If you have a custom class that you are using to create the layout of your panels then you might wish to handle your events from your main class.
- *Although this takes a bit of thinking to figure out, the logic of this is sound and it does work.* **SHOULD WORK**
- For our example here, assume that we have a class named OurPanel() which extends Panel, OurPanel() creates a panel which contains a checkbox that we want our main class to handle events from. assume we have already made panel1 = new OurPanel() and panel2 = new OurPanel() *both earlier just Panels*
- to add a listener to handle events from both panel1's and panel2's checkboxes we must add the following code: for (int i = 1; i <= this.getComponents().length - 1; i++){Panel tP = new Panel(); tP = ((Panel)this.getComponent(i - 1)); for (int j = 1; j <= tP.getComponents().length - 1; j++){if(tP.getComponents()[j - 1] instanceof Checkbox){((Checkbox)tP.getComponents()[j - 1]).addItemListener(this);}}}
Objective #5: Switching panels
- To switch your panel you simply use the code: cardLayout.show(this, "panel1); *this will result in panel one being brought to the front.*