12 Replies - 1139 Views - Last Post: 08 June 2012 - 06:20 PM Rate Topic: -----

#1 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

problem JComboBox

Posted 08 June 2012 - 11:55 AM

Hello i really need help with some stuff about JComboBox let me just show you the code.
import javax.swing.JComboBox;


public class PlayerSkill {
	
   public int damage = 0;
   public int manaCost = 0;
   public int ID = 0;
   
   public static int skills[]={0,0,0,0};
   public static String skillName[]={"skills", "", "", "", ""};
   private JComboBox skillList = new JComboBox(skillName);
	
	public PlayerSkill(int damage, int manaCost, int ID){
		
		Gui.f.add(skillList);
		
		skillList.setBounds(100,100,200,200);
		
		this.damage = damage;
		this.manaCost = manaCost;
		this.ID = ID;
	}
	
	public void addNewItem(){
		
		for(int i = 1; i < skills.length; i++){
			
			if(skills[i] == 0 && skillName[i] == ""){
				
					skillList.insertItemAt(skillName(), i);
					skills[i] = ID;
					
					return;
			}
			System.out.println("Test1" + i);
		}
	}
	
	//Translates item IDs into item names.
	public String skillName(){
		
		switch(ID){
		case 0: return "epic smile";
		case 1: return "rain bow blast";
		case 2: return "bro hoof";
		case 3: return "gooby pls";
		}
		
		return "";
	}
}

and here is this code
Player.playerRace = "EPIC_FACE";
							PlayerSkill epic_smile1 = new PlayerSkill(4,1,1);
							PlayerSkill epic_smile = new PlayerSkill(4,1,0);
							epic_smile.addNewItem();
							epic_smile1.addNewItem();

It should be printing: skill, epic smile, rain bow blast. But it only prints: skill, and rain bow blast.

Any help would be nice:) Thank you, I really need this done:(

Is This A Good Question/Topic? 0
  • +

Replies To: problem JComboBox

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: problem JComboBox

Posted 08 June 2012 - 12:00 PM

skillName[i] == ""){

You can't compare Object (and String are object) withh the == operator
You need to use the equals() method

skillName[i].equals(""){

or faster in that case: skillName[i].length() == 0)
Was This Post Helpful? 0
  • +
  • -

#3 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 12:05 PM

Did this
	public void addNewItem(){
		
		for(int i = 1; i < skills.length; i++){
			
			if(skills[i] == 0 && skillName[i].equals("")){
				
					skillList.insertItemAt(skillName(), i);
					skills[i] = ID;
					
					return;
			}
			System.out.println("Test1" + i);
		}
	}

Still did not work.
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: problem JComboBox

Posted 08 June 2012 - 12:22 PM

I haven't checked it but your for-loop starts from i=1!
Was This Post Helpful? 0
  • +
  • -

#5 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 12:25 PM

View Postletthecolorsrumble, on 08 June 2012 - 12:22 PM, said:

I haven't checked it but your for-loop starts from i=1!

Sorry yes i just fixed that/: And still does not work:( Thanks for pointing it out tho!
Was This Post Helpful? 0
  • +
  • -

#6 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: problem JComboBox

Posted 08 June 2012 - 12:30 PM

Are you passing the ID value to the skillName() method for the ID? (for switch case)

I'm really sorry I do not have an IDE on this machine so can't really help you as quickly as I could.

This post has been edited by letthecolorsrumble: 08 June 2012 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 12:50 PM

No but i do not think i need too.
Was This Post Helpful? 0
  • +
  • -

#8 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: problem JComboBox

Posted 08 June 2012 - 01:10 PM

True, you don't need to, its public. My Bad!
Was This Post Helpful? 0
  • +
  • -

#9 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 01:14 PM

Its fine :) I just wish i could get this to work;(
Was This Post Helpful? 0
  • +
  • -

#10 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: problem JComboBox

Posted 08 June 2012 - 01:57 PM

Just a guess, but could the error be in

line 29
if(skills[i] == 0

line 32
skills[i] = ID;


hope that helps!
Was This Post Helpful? 0
  • +
  • -

#11 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 02:09 PM

View Postletthecolorsrumble, on 08 June 2012 - 01:57 PM, said:

Just a guess, but could the error be in

line 29
if(skills[i] == 0

line 32
skills[i] = ID;


hope that helps!

I don't think that could be it cause in if statements you need to do == and just regular you do =.
Was This Post Helpful? 0
  • +
  • -

#12 buddyboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-June 12

Re: problem JComboBox

Posted 08 June 2012 - 02:58 PM

Will any one help me to fix this please:(
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: problem JComboBox

Posted 08 June 2012 - 06:20 PM

Why don't you rather tell us what you are trying to do ?
Why filling a ComboBox with empty String "" ?

When you insertItemAt() you insert there but you push down the empty String

Why do you need a skills[] and a skillName[] array ? Why are they static ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1