From: Amr on 14 Feb 2010 04:24 hi all, im following an excercise from the book "Sams teach yourself java in 21 days" i typed the exercise exactly as in the book but i get an error. below are the error and the full code of the program. thank you very much for your help. error: Caused by: java.lang.RuntimeException: Uncompilable source code - DayEleven.SurveryWizard is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener im doing this excercise in Netbeans and it gives an option to 'implement all abstract methods' to get rid of this error. doing that allows the compilation, but can't when go forward with the second interface. package DayEleven; import java.awt.event.*; import javax.swing.*; import java.awt.*; /** * * @author amr */ public class SurveryWizard extends JPanel implements ActionListener{ int currentCard=0; CardLayout cards=new CardLayout(); SurveyPanel[] ask=new SurveyPanel[3]; public SurveryWizard(){ super(); setSize(240, 140); //set up survery String question1="What is your gender"; String[] response1={"female","male","not telling"}; ask[0]=new SurveyPanel(question1,response1,2); String question2="What is your age"; String[] response2={"Under 25","25-34","35-40","over 54"}; ask[1]=new SurveyPanel(question1,response2,1); String question3="How often do you excercise each week"; String[] responses3={"Never", "1-3 times","More than 3"}; ask[2]=new SurveyPanel(question3,responses3,1); ask[2].setFinalQuestion(true); for(int i=0; i<ask.length;i++){ ask[i].nextButton.addActionListener(this); ask[i].finalButton.addActionListener(this); add(ask[i],"Card",i); } } public void actionPerformed(ActiveEvent evt){ currentCard++; if(currentCard>=ask.length){ System.exit(0); } cards.show(this,"Card "+currentCard); } } class SurveyPanel extends JPanel{ JLabel question; JRadioButton[] response; JButton nextButton=new JButton("Next"); JButton finalButton=new JButton("Finish"); public SurveyPanel(String ques, String[] resp, int def) { super(); setSize(160, 110); question =new JLabel(ques); response=new JRadioButton[resp.length]; JPanel sub1=new JPanel(); ButtonGroup group=new ButtonGroup(); JLabel quesLabel=new JLabel(ques); sub1.add(quesLabel); JPanel sub2=new JPanel(); for(int i=0; i<resp.length;i++){ if(def==i){ response[i]=new JRadioButton(resp[i],true); }else{ response[i]=new JRadioButton(resp[i],false); } group.add(response[i]); sub2.add(response[i]); } JPanel sub3=new JPanel(); nextButton.setEnabled(true); sub3.add(nextButton); finalButton.setEnabled(false); sub3.add(finalButton); GridLayout grid=new GridLayout(3, 1); setLayout(grid); add(sub1); add(sub2); add(sub3); } void setFinalQuestion(boolean finalQuestion){ if(finalQuestion){ nextButton.setEnabled(false); finalButton.setEnabled(true); } } } package DayEleven; import javax.swing.JFrame; /** * * @author amr */ public class SurveyFrame extends JFrame{ public SurveyFrame(){ super("Survery"); setSize(290, 140); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SurveryWizard wiz=new SurveryWizard(); add(wiz); setVisible(true); } static public void main(String arg[]){ SurveyFrame surv=new SurveyFrame(); } }
From: rossum on 14 Feb 2010 07:40 On Sun, 14 Feb 2010 01:24:55 -0800 (PST), Amr <fromwindowstolinux(a)gmail.com> wrote: >hi all, >im following an excercise from the book "Sams teach yourself java in >21 days" >i typed the exercise exactly as in the book but i get an error. below >are the error and the full code of the program. >thank you very much for your help. > >error: >Caused by: java.lang.RuntimeException: Uncompilable source code - >DayEleven.SurveryWizard is not abstract and does not override abstract >method actionPerformed(java.awt.event.ActionEvent) in >java.awt.event.ActionListener This is telling you that your class DayEleven.SurveryWizard does not correctly override method actionPerformed(java.awt.event.ActionEvent) That is where you need to look first for your error. > >im doing this excercise in Netbeans and it gives an option to >'implement all abstract methods' to get rid of this error. >doing that allows the compilation, but can't when go forward with the >second interface. > >package DayEleven; > >import java.awt.event.*; >import javax.swing.*; >import java.awt.*; > > > >/** > * > * @author amr > */ >public class SurveryWizard extends JPanel implements ActionListener{ > int currentCard=0; > CardLayout cards=new CardLayout(); > SurveyPanel[] ask=new SurveyPanel[3]; > > public SurveryWizard(){ > super(); > setSize(240, 140); > //set up survery > > String question1="What is your gender"; > String[] response1={"female","male","not telling"}; > ask[0]=new SurveyPanel(question1,response1,2); > String question2="What is your age"; > String[] response2={"Under 25","25-34","35-40","over 54"}; > ask[1]=new SurveyPanel(question1,response2,1); > String question3="How often do you excercise each week"; > String[] responses3={"Never", "1-3 times","More than 3"}; > ask[2]=new SurveyPanel(question3,responses3,1); > ask[2].setFinalQuestion(true); > for(int i=0; i<ask.length;i++){ > ask[i].nextButton.addActionListener(this); > ask[i].finalButton.addActionListener(this); > add(ask[i],"Card",i); > } > } > So, here is where you need to start looking. > public void actionPerformed(ActiveEvent evt){ Look at the type of the parameter you are using here: ActiveEvent Now go back and look at the typr of the parameter of the method you are trying to override: ActionEvent You are not overriding actionPerformed() but are overloading it instead, which is probably not what you intended to do. rossum > currentCard++; > if(currentCard>=ask.length){ > System.exit(0); > } > cards.show(this,"Card "+currentCard); > } > > [snip code]
From: Amr on 14 Feb 2010 11:34 Thank you very much for the reply. it solved part of the problem. now i can't move to the next card. and i get this error: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout at java.awt.CardLayout.checkLayout(CardLayout.java:404) at java.awt.CardLayout.show(CardLayout.java:526) at DayEleven.SurveryWizard.actionPerformed(SurveryWizard.java: 50) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java: 2012) at javax.swing.AbstractButton $Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java: 404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java: 253) at java.awt.Component.processMouseEvent(Component.java:6108) at javax.swing.JComponent.processMouseEvent(JComponent.java: 3267) at java.awt.Component.processEvent(Component.java:5873) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4469) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Container.java: 4055) at java.awt.Container.dispatchEventImpl(Container.java:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.EventQueue.dispatchEvent(EventQueue.java:604) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java: 275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java: 200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java: 190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java: 138) and it says,the error is in the last line of this code snippet: public void actionPerformed(ActionEvent evt){ currentCard++; if(currentCard>=ask.length){ System.exit(0); } cards.show(this,"Card "+currentCard); } thank you very much for your time.
From: Amr on 14 Feb 2010 11:35 Thank you very much for the reply. it solved part of the problem. now i can't move to the next card. and i get this error: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout at java.awt.CardLayout.checkLayout(CardLayout.java:404) at java.awt.CardLayout.show(CardLayout.java:526) at DayEleven.SurveryWizard.actionPerformed(SurveryWizard.java: 50) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java: 2012) at javax.swing.AbstractButton $Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java: 404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java: 253) at java.awt.Component.processMouseEvent(Component.java:6108) at javax.swing.JComponent.processMouseEvent(JComponent.java: 3267) at java.awt.Component.processEvent(Component.java:5873) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4469) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Container.java: 4055) at java.awt.Container.dispatchEventImpl(Container.java:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.EventQueue.dispatchEvent(EventQueue.java:604) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java: 275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java: 200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java: 190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java: 138) and it says,the error is in the last line of this code snippet: public void actionPerformed(ActionEvent evt){ currentCard++; if(currentCard>=ask.length){ System.exit(0); } cards.show(this,"Card "+currentCard); } thank you very much for your time.
From: markspace on 14 Feb 2010 14:16
Amr wrote: > Thank you very much for the reply. > it solved part of the problem. > now i can't move to the next card. and i get this error: > > Exception in thread "AWT-EventQueue-0" > java.lang.IllegalArgumentException: wrong parent for CardLayout This bit from your code that starts here seems wrong to me: CardLayout cards=new CardLayout(); SurveyPanel[] ask=new SurveyPanel[3]; You don't use "cards" anywhere. Usually you have to set a container with the required layout. You don't do this anywhere that I can see. Maybe this is the "wrong parent," since the "this" doesn't have a CardLayout? Also, it seems wrong to keep each of the panels around separately, like you do in the array named "ask". It seems CardLayout should take care of holding those panels. Not sure about that however. I've never had a good experience with Sam's books. Take a look at the Java tutorial from Sun (nee Oracle) and their example, which is a bit different in how it sets stuff up. <http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html> <http://java.sun.com/docs/books/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java> |