Jcombobox and switch case - switch-statement

I want to link my combobox selected item with switch case which result in different result depend on option chosen. But return in this error :Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to java.awt.event.ItemListener. Can only one help me, thanks you so much!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1.addItemListener((ItemListener) jComboBox1);
int usertype = (int) jComboBox1.getSelectedIndex();
switch (usertype) {
case '1':
new ResidentView().setVisible(true);
this.dispose();
break;
case '2':
new OfficeClerkView().setVisible(true);
this.dispose();
break;
case '3':
new OfficeManagerView().setVisible(true);
this.dispose();
break;
}

Related

Stuck on virtual zoo

I'm doing a virtual zoo in computer science and i'm stuck. We are using a guide to navigate through the zoo, but I can't figure out how to go without getting errors. When I run it it says "Null! Null, and I have no idea what to do. I am pretty new to coding so thank you for your help.
class Guide {
private String name;
int Selection;
int Selection1;
int Selection3;
int Selection2;
int choice;
Scanner inputScanner = new Scanner (System.in);
void welcomeusertozoo() {
System.out.println("Hello! Welcome to Cringe Jarin! A Zoo full of cringy creative creatures. Im Fred. What is your Cringy Name?");
name = inputScanner.next();
System.err.println("Ewww..... How cringy. Well, Welcome " + name + "! What creatures would you like to visit First?"
+ "\n 1.Ogres"
+ "\n 2.Vampires"
+ "\n 3.Rats \n\n" );
choice = inputScanner.nextInt();
creatureselection(choice);
}
void creatureselection(int Selection) {
switch (Selection) {
case 1: System.out.println("Plug up the nose and close those eyes! These creatures are Ugly! who Would you Like to visit...\n Please press 1 to go meet the commander himself, Ogre E Lee. \n Please press 2 to go meet the one and only Ogrewall Jackson. \n Please press 3 to meet the wonderful Mr.GreenBean. \n Please press 4 to meet the rebellious William Shrallace. \n\n");
choice = inputScanner.nextInt();
switch (choice){
case 1: System.out.println("Commander Ogre. E Lee Here! \n\n");
case 2: System.out.println("This is the impeccable, the amazing, one and only Ogrewall Jackson \n\n");
break;
case 3: System.out.println("This is the fashionable, the sexy, the fabulous GreenBean \n\n");
break;
case 4: System.out.println("This is the free, the bravest heart I know, the impecable William Shrallace \n\n");
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
}
break;
case 2:System.out.println("Dont have a scarf? Oh.... watch your necks! Who would you like to visit...\n Please press 1 to go meet the spooky Dio. \n Please press 2 to go meet the truly bone chilling Diego. \n Please press 3 to meet the bloodthirsty Big White Bat. \n Please press 4 to meet the Bold Mubat. \n\n");
choice = inputScanner.nextInt();
switch (choice){
case 1: System.out.println("Dio \n\n"); creatureselection(2);
break;
case 2: System.out.println("Diego \n\n"); creatureselection(2);
break;
case 3: System.out.println("Big White bat \n\n"); creatureselection(2);
break;
case 4: System.out.println("Bold Mubat \n\n"); creatureselection(2);
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(2);
}
break;
case 3:System.out.println("Pull out the Cheese but dont let it rip! These creatures are Cringy! Who would you like to visit...\n Please press 1 to go meet the Odd Ratty Weisau. \n Please press 2 to go meet the backstabbing Risa. \n Please press 3 to meet the permanently sad Dindin. \n Please press 4 to meet the sketchy Markymouse. \n\n");
choice = inputScanner.nextInt();
switch (choice) {
case 1: System.out.println("Ratty Weisau \n\n"); creatureselection(3);
break;
case 2: System.out.println("Risa \n\n"); creatureselection(3);
break;
case 3: System.out.println("Dindin \n\n"); creatureselection(3);
break;
case 4: System.out.println("Markymouse \n\n"); creatureselection(3);
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(3);
}
break;
default: System.out.println("We do not have a creature under that selection at the moment. Please choose 1 or 2 or 3 \n\n");
creatureselection(1);
}
}
void setName(String fred) {
}
}
What seems to be the problem?
Can you provide more information on what is the problem.
It seems to be working.
Are you creating a Guide object?
public class test {
public static void main(String[] args) {
Guide guide = new Guide();
guide.welcomeusertozoo();
}
}
After that everything seems to be working.
Just make sure that you have breaks on every case, just to assure consistency.
Hope I've helped.
This is the main :
public class ZooProject {
public static void main(String[] args) {
Rat RattyWiseau = new Rat ();
Ogre OgreELee = new Ogre();
Vampire Dio = new Vampire ();
Guide Fredtheguide = new Guide ();
//ask user for name
RattyWiseau.setName("Ratty Wiseau");
Dio.setName("Dio");
// Fredtheguide.setName("Fred");
OgreELee.setName("Ogre E Lee");
Fredtheguide.welcomeusertozoo();
System.out.println(OgreELee.getName());
}
}

ClassCastException with handleMessage

Debugging bit of code for bluetooth connection receive string etc. However, seem to be getting a ClassCastException at the line of code
String read_Message = (String) msg.obj;
But if I was to use my previous bit of code and collect the bytes and place into a string runs but doesn't collect all the data in one string.
Have I not cast something or missed something cause I know I'm missing something but can't see it.
If any more code is required I will place up, everything else is working
Thanks for any help
// The Handler that gets information back from the BluetoothService
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MESSAGE_STATE_CHANGE:
// if (D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1)
{
case BluetoothService.STATE_CONNECTED:
break;
case BluetoothService.STATE_CONNECTING:
//mTitle.setText(R.string.title_connecting);
break;
case BluetoothService.STATE_LISTEN:
case BluetoothService.STATE_NONE:
//mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
//code to be entered here
break;
case MESSAGE_READ:
//Previous code
//byte[] read_Buf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
//String read_Message = new String(read_Buf, 0, msg.arg1);
String read_Message = (String) msg.obj;
if (mSmokeReadingArrayAdapter.isEmpty())
mSmokeReadingArrayAdapter.add("");
mAdapter_Text.set(0, mAdapter_Text.get(0).toString() + read_Message);
mSmokeReadingArrayAdapter.notifyDataSetChanged();
organiseString(read_Message);
break;
case MESSAGE_DEVICE_NAME:
//code to be entered here
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show(); // shows 'alert' messages
break;
}
}
};
Problem solved. It wa to do with the bluetooth service code that had been defined as a byte array rather than string

Default statement position in switch

switch(2){
default : System.out.println("I am Default block");
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
}
The output : in 2
and if
switch(2){
case 1 : System.out.println("in 1");
case 2 : System.out.println("in 2");
default : System.out.println("I am Default block");
}
And the output is: in 2
I am Default block.
Does the positioning of the default statement behaves differently?
It's not the position of the default block, it's the use (or non-use in your case) of the break statement.
Once a case has been matched, the default behaviour is for the code to cascade through the rest of the cases.
To achieve the behaviour you desire, try
switch (2)
{
case 1:
System.out.println("in 1");
break;
case 2:
System.out.println("in 2");
break;
default:
System.out.println("I am Default block");
break;
}
Kathy Siera SCJP book do say that position of default: matters and if present in between the case statements, it follows the fall-through.
But practically it does not follow.
int a = 10;
switch (a)
{
case 5:
System.out.println("Its 5");
default:
System.out.println("Its default");
case 10:
System.out.println("Its 10");
case 20:
System.out.println("Its 20");
}
Output:
Its 10
Its 20

Pivot Control ManipulationCompleted

ManipulationCompleted event doesn't work in WP8 SDK. What would be an alternative way to solve this problem?
Including dectecting swipe right or left, or choose other pivot item by clicking on the pivot header.
You can use SelectedIndex to identified selected pivot page. For instance, you can use as:
private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (((Pivot)sender).SelectedIndex)
{
case 0:
ApplicationBar = ((ApplicationBar)Resources["AppBar1"]);
break;
case 1:
ApplicationBar = ((ApplicationBar)Resources["AppBar2"]);
break;
case 2:
ApplicationBar = ((ApplicationBar)Resources["AppBar3"]);
break;
case 3:
ApplicationBar = ((ApplicationBar)Resources["AppBar4"]);
break;
}
}
Hope this help

Get pressed keys in J2ME with GameCanvas

I would like to get whether (for example) the 3 key is pressed (KEY_NUM3).
I have tried getKeyStates but it only detects the game action keys.
How could I get the states of non-game action keys?
(I have overridden the keyPressed and keyReleased functions of Canvas and storing the key states in an array (I'm using a Vector for storing but I think could store them in an array too, if that's the problem), but this does not seem to be very nice)
in your keypressed use the keyCode passed in like so
protected void keyPressed(int keyCode)
{
//try catch getGameAction as can legally throw an exception
int gameAction = getGameAction(keyCode);
switch(gameAction)
{
case UP:
break;
case DOWN:
break;
case LEFT:
break;
}
switch(keyCode)
{
case KEY_NUM1:
break;
case KEY_NUM2:
break;
case KEY_NUM3;
break;
}
}
I suppose that can be
something like the code below
int key=getKeyStates();
// i mean keyStates();
if((key&down_pressed)!=0)
{
//do movements
}
but can be
if((key & Canvas.key_num3)!=0)
{
//do something
}
//you can set the super() to true in the constructor

Resources