need help with JButton and switch statements
Hello Dalisra,
I hope u can help me.
I am also kinda new with Java, I still need my Sun exams behind my name so Im not a registered programmer yet.
Im trying to build a program for myself and trying to put in new statements but have a problem with finishing it
and Im looking for answers on the Internet but currently havent found anything yet.
My problem is and I hope u can help me with this one, its also with switch statement. Its about using JButtons
inside a switch statement. Each button u click on makes a choice in ur switch statement.
Kinda know one must use an enum but dont totally know what to do all the different methods and stuff that has
to be used.
If u can PLEASE help me with this one Dalisra I will be very Pleased.
Thanks Jean.
Re: Switch statement question
Quote:
Originally Posted by
jjoubert
Hello Dalisra,
I hope u can help me.
I am also kinda new with Java, I still need my Sun exams behind my name so Im not a registered programmer yet.
Im trying to build a program for myself and trying to put in new statements but have a problem with finishing it
and Im looking for answers on the Internet but currently havent found anything yet.
My problem is and I hope u can help me with this one, its also with switch statement. Its about using JButtons
inside a switch statement. Each button u click on makes a choice in ur switch statement.
Kinda know one must use an enum but dont totally know what to do all the different methods and stuff that has
to be used.
If u can PLEASE help me with this one Dalisra I will be very Pleased.
Thanks Jean.
Hey jjoubert.
Please create a new thread.. I'm sure someone will be able to solve your problem! :)
Re: Switch statement question
Hello JavaPF,
OK here is my kinda exact problem.
PLEASE if u can help me with this?
as u will see following is the enum I created for the 4 buttons:
Code :
enum ChoiceMarker{cvButton, workButton, personalButton, exitButton}
here following are the creation of the 4 buttons:
Code :
JButton cvButton = new JButton("Qualifications");
JButton workButton = new JButton("Knowledge of Work");
JButton personalButton = new JButton("Jean's Preferences");
JButton exitButton = new JButton("Exit");
Now this is where the problem is. I cant find a way to declare the JButtons to Integer
for use with the switch statement.
Code :
Object source = e.getSource();
ChoiceMarker button = Integer.parseInt(source);
switch (button)
{
case cvButton : JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();break;
case workButton : JeansLikingOfWork jeanslw = new JeansLikingOfWork();break;
case personalButton : JeansePreferences jeansep = new JeansePreferences();break;
case exitButton : ExitFully();break;
}
this is the method I used and it works perfectly but I want to come to know
the switch statement...
Code :
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == cvButton)
{
JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
}
else if (source == workButton)
{
JeansLikingOfWork jeanslw = new JeansLikingOfWork();
}
else if (source == personalButton)
{
JeansePreferences jeansep = new JeansePreferences();
}
else if (source == exitButton)
{
ExitFully();
}
I hope this help u with trying to help me with this problem.
Just ask if I can send anything more..?!?!
Thanks till this far
Re: need help with JButton and switch statements
Java doesn't support "de-facto" casts between integers and enums. I know you can go from Enums to integers by using the ordinal() method, I don't think you can the other way around without writing your own method for the conversion. There is a method inside the Java Enum class that lets you take a String input and convert it to the matching Enum. I'm going to assume that the button's name is the same as the Enum specified.
Also, fyi it's good practice to have 1 statement per line at most (sometimes, multiple lines per statement).
Code :
ChoiceMarker button = ChoiceMarker.valueOf(e.getActionCommand());
switch (button)
{
case cvButton :
JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
break;
case workButton :
JeansLikingOfWork jeanslw = new JeansLikingOfWork();
break;
case personalButton :
JeansePreferences jeansep = new JeansePreferences();
break;
case exitButton :
ExitFully();
break;
}
Re: need help with JButton and switch statements
Quote:
Originally Posted by
helloworld922
Java doesn't support "de-facto" casts between integers and enums. I know you can go from Enums to integers by using the ordinal() method, I don't think you can the other way around without writing your own method for the conversion. There is a method inside the Java Enum class that lets you take a String input and convert it to the matching Enum. I'm going to assume that the button's name is the same as the Enum specified.
Also, fyi it's good practice to have 1 statement per line at most (sometimes, multiple lines per statement).
Code :
ChoiceMarker button = ChoiceMarker.valueOf(e.getActionCommand());
switch (button)
{
case cvButton :
JeansFirstCV_Program jeansCV = new JeansFirstCV_Program();
break;
case workButton :
JeansLikingOfWork jeanslw = new JeansLikingOfWork();
break;
case personalButton :
JeansePreferences jeansep = new JeansePreferences();
break;
case exitButton :
ExitFully();
break;
}
Hello World,
Thx very much for ur help concerning this. So SORRY coming back to u now but sorry
been in hospital from monday till today.
I tried it out and thx the file now compiles correctly thx but wont open up the class concerning the JButton
clicked upon. Dont really know anything about the new type of code you have given me now if u can just
PLEASE help here..??
Thx and hope to hear soon.
from Jean.
Re: need help with JButton and switch statements
Me thinks you forgot to add your class to be an action listener for all the buttons?
Code :
jeans_CV_Button.addActionListener(this);
... etc