Problem with interface implementation


A

Amr

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.nextButton.addActionListener(this);
ask.finalButton.addActionListener(this);
add(ask,"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=new JRadioButton(resp,true);
}else{
response=new JRadioButton(resp,false);
}

group.add(response);
sub2.add(response);
}

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();
}

}
 
Ad

Advertisements

A

Amr

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.
 
A

Amr

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.
 
M

markspace

Amr said:
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/tuto...outDemoProject/src/layout/CardLayoutDemo.java>
 
M

Mike Schilling

Lew said:
That should be "Oracle (nee Sun)", since Java was born with family
name "Sun" and then married into the "Oracle" family.

The correct use of "nee" can be found at
 
J

Joshua Cranmer

That should be "Oracle (nee Sun)", since Java was born with family name
"Sun" and then married into the "Oracle" family.

To be most pedantic, it's né or née, depending on whether or not you
consider Java to be masculine or feminine. It's the past participle of
the French word 'naître', or 'to be born.'
 
Ad

Advertisements

L

Lew

Joshua said:
To be most pedantic, it's né or née, depending on whether or not you
consider Java to be masculine or feminine. It's the past participle of
the French word 'naître', or 'to be born.'

You are absolutely correct. I've received a lot of grief over being overly
pedantic in these fora, so I forbore to take it to that level. Nevertheless I
heartily endorse your observation.

Clearly Java must be feminine, as she's the one who changed her family name
when the marriage happened.
 
T

Tom Anderson

You are absolutely correct. I've received a lot of grief over being overly
pedantic in these fora, so I forbore to take it to that level. Nevertheless
I heartily endorse your observation.

Clearly Java must be feminine, as she's the one who changed her family
name when the marriage happened.

Now there's an assumption and a half! Java could well be feminine and
traditional, but could also be masculine and modern. A male friend of mine
is planning to adopt his fiancee's surname when they marry.

The real problem here, of course, is the French. Why does everything have
to be either male or female? Very silly. Hopefully we'll see this fixed in
the next major release.

tom
 
L

Lew

Tom said:
Now there's an assumption and a half! Java could well be feminine and
traditional, but could also be masculine and modern. A male friend of
mine is planning to adopt his fiancee's surname when they marry.

You could call it an assumption. I call it playing the odds. Your
(putatively) male friend is in a decided minority.

If you find out that someone changed their family name to their spouse's upon
marriage, without any further information, and had to place a bet on the
gender of that person, you are far, far more likely to win if you bet that the
person is female.

So until you provide evidence to the contrary, I'll go with Java née Sun.

Although the Oracle of Delphi was female. Hmm.
 
A

Alessio Stalla

You could call it an assumption.  I call it playing the odds.  Your
(putatively) male friend is in a decided minority.

If you find out that someone changed their family name to their spouse's upon
marriage, without any further information, and had to place a bet on the
gender of that person, you are far, far more likely to win if you bet that the
person is female.

So until you provide evidence to the contrary, I'll go with Java née Sun.

Although the Oracle of Delphi was female.  Hmm.

Italian has masculine/feminine gender distinctions for nouns and
adjectives, too.
Now Java is a programming language, and language ("linguaggio") is
male in Italian.
But Java is an island, too, and island ("isola") is female...
And Oracle ("oracolo") is male, even if there are female oracles, like
the Oracle of Delphi.

I wonder if in French it is so messy... ;)
 
R

Robert Klemme

According to Alessio Stalla said:
Italian has masculine/feminine gender distinctions for nouns and
adjectives, too.
Now Java is a programming language, and language ("linguaggio") is
male in Italian.
But Java is an island, too, and island ("isola") is female...
And Oracle ("oracolo") is male, even if there are female oracles, like
the Oracle of Delphi.

I wonder if in French it is so messy... ;)

In French, genders are the same than in Italian for these nouns. It does
not feel messy at all. French does not have a neutral gender, so every
single noun is either masculine or feminine (with a few odd idiomatic
exceptions such as "gens" ["people"], which is feminine on the left and
masculine on the right, and "amour" ["love"], which switches gender when
made plural -- an everlasting source of jokes for bored linguists after
too alcoholic a meal). When all inanimate objects have a gender, the
endless american debates on how to avoid sexism in speach tend to look a
bit ridiculous.

In everyday talk between programmers, programming languages, including
Java, are masculine, and nobody finds it the slightest odd.

Also, French law makes it legal for the husband to use his wife's name,
if he wishes so.

May I throw in another language? In German we have masculine, feminine
and neuter. Also, I believe not the oracle was male or female: the
voices of the oracle were female. Which gives a certain indication as
to the gender of Python. But what does it tell us about the gender of
PL/SQL? In German coffee is male so Java might be male as well...

robert
 
Ad

Advertisements

M

Mike Schilling

Robert said:
May I throw in another language? In German we have masculine,
feminine and neuter. Also, I believe not the oracle was male or
female: the voices of the oracle were female. Which gives a certain
indication as to the gender of Python. But what does it tell us
about the gender of PL/SQL? In German coffee is male so Java might
be male as well...

Though if you created a subset of Java, say for teaching children to
program, it would be neuter, assuming it's called Javalein or Javachen.
 
R

Robert Klemme

Though if you created a subset of Java, say for teaching children to
program, it would be neuter, assuming it's called Javalein or Javachen.

Isn't that "JavaScript"?

robert
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top