Switching between cards in a card layout
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Semih_Altinay
Posted On:   Friday, August 23, 2002 08:30 AM

Here is my code: ... Panel optionPanel = new Panel(); Label viewLabel = new Label("Viewing Options:"); Choice choice = new Choice(); choice.addItem(TEXTPANEL); choice.addItem(GRAPHPANEL); ... cardPanel = new Panel(); cardPanel.setLayout(new CardLayout()); TP = new TextPanel(); GP = new GraphPanel(); cardPanel.add(TEXTPANEL, TP); cardPanel.add(GRAPHPANEL, GP); gridBag.setConstraints(cardPanel, c); add(cardPanel); ... public boolean action(Event e, Object arg) { if(e.target instanceof Choice) { ((CardLayout)cardPanel.getLayout()).show(cardPanel, (String)arg); return true; }    More>>

Here is my code:

			
...
Panel optionPanel = new Panel();
Label viewLabel = new Label("Viewing Options:");
Choice choice = new Choice();
choice.addItem(TEXTPANEL);
choice.addItem(GRAPHPANEL);
...
cardPanel = new Panel();
cardPanel.setLayout(new CardLayout());
TP = new TextPanel();
GP = new GraphPanel();
cardPanel.add(TEXTPANEL, TP);
cardPanel.add(GRAPHPANEL, GP);
gridBag.setConstraints(cardPanel, c);
add(cardPanel);
...
public boolean action(Event e, Object arg)
{
if(e.target instanceof Choice)
{
((CardLayout)cardPanel.getLayout()).show(cardPanel, (String)arg);
return true;
}
return false;
}

It works wonderfully, but "action" is deprecated. What should I use instead of this method?

   <<Less

Re: Switching between cards in a card layout

Posted By:   John_Ewing  
Posted On:   Friday, August 23, 2002 10:21 AM

From Sun's javadoc page: As of JDK version 1.1, should register this component as ActionListener on component which fires action events.


http://java.sun.com/j2se/1.3/docs/api/java/awt/Component.html#action(java.awt.Event, java.lang.Object)


Here is some code to get you started:

public class j{
...
Button move = new Button(moveButtonLabel);
move.addActionListener(new myActionListener());
...
public class myActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button b = (Button)e.getSource();
if (b.getLabel().equals(moveButtonLabel)) {
doSomething();
}
...
}
}// end inner class myActionListener

}// end class j



Good Luck!


~John
About | Sitemap | Contact