java - How can I read a String array index from a JComboBox into a JLabel? -


i have combobox holding string[]'s values. have several other string[]'s holding numbers. want user able select item combobox(holding string[] names) , according 1 pick, want index associated 1 of other arrays printed out in jlabel display. here's have far:

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*;  public class newbuild extends jframe { private static final int width = 900; private static final int height = 350;  //create array constants private static final string[] warrior = {"7","6","6","5","15","11","5","5","5"}; private static final string[] knight = {"12","6","7","4","11","8","9","3","6"}; private static final string[] swordsman =   {"4","8","4","6","9","16","6","7","5"}; private static final string[] bandit = {"9","7","11","2","9","14","3","1","8"}; private static final string[] cleric =  {"10","3","8","10","11","5","4","4","12"}; private static final string[] sorcerer = {"5","6","5","12","3","7","8","14","4"}; private static final string[] explorer = {"7","6","9","7","6","6","12","5","5"}; private static final string[] deprived = {"6","6","6","6","6","6","6","6","6"}; private static final string[] class_names = {" ", "warrior", "knight", "swordsman", "bandit", "cleric", "sorcerer", "explorer", "deprived"};    private int num; private int count = 0; private string classes;  container newbuildwindow = getcontentpane();  private jlabel lblbuildname, lblstartingclass, lbldisplaystartingstats;  private jtextfield txtbuildname;  private jcombobox cstartingclasses;   public newbuild() {     gui();  }//end of constructor  public void gui() {     this.setsize(width, height);     newbuildwindow.setbackground(color.dark_gray);     settitle("new build");     setlayout(new gridlayout(5,2));     setvisible(true); //  setdefaultcloseoperation(exit_on_close);     this.setlocationrelativeto(null);      jpanel panel1 = new jpanel(new gridlayout());     jpanel panel2 = new jpanel(new gridlayout(2,3));      lblbuildname = new jlabel("build name");     lblbuildname.setforeground(color.yellow);     panel1.setbackground(color.dark_gray);     panel1.add(lblbuildname);      txtbuildname = new jtextfield(15);     txtbuildname.setbackground(color.light_gray);     panel1.add(txtbuildname);      lblstartingclass = new jlabel("pick starting class:");     lblstartingclass.setforeground(color.yellow);     lbldisplaystartingstats = new jlabel("default");     lbldisplaystartingstats.setforeground(color.yellow);     cstartingclasses = new jcombobox(class_names);      cstartingclasses.additemlistener(new itemchangelistener());        panel2.setbackground(color.dark_gray);     panel2.add(lblstartingclass);     panel2.add(cstartingclasses);     panel2.add(lbldisplaystartingstats);       //add panels pane     newbuildwindow.add(panel1);     newbuildwindow.add(panel2); //  pack();  }//end of gui method  class itemchangelistener implements itemlistener {     @override     public void itemstatechanged(itemevent e)     {         if(e.getstatechange() == itemevent.selected)         {            }      }  }       }//end of class newbuild 

any appreciated...please don't post solution, want understand code not copy it.

first, create "wrapper" class can bind string values particular name (before rushes @ me says can use map of kind, could, "carries" associated information within neat package)

public class classtype {      private string description;     private string[] values;      public classtype(string description, string[] values) {         this.description = description;         this.values = values;     }      public string[] getvalues() {         return values;     }      @override     public string tostring() {         return description;     }  } 

build array of these classtypes , give jcombobox

    classtype[] types = new classtype[]{         new classtype("warrior", warrior),         new classtype("knight", knight),         new classtype("swordsman", swordsman),         new classtype("bandit", bandit),         new classtype("cleric", cleric),         new classtype("socerer", sorcerer),         new classtype("explorer", explorer),         new classtype("deprived", deprived)     };      cstartingclasses = new jcombobox(types); 

and when itemlistener notified, extract values selected item...

@override public void itemstatechanged(itemevent e) {     if (e.getstatechange() == itemevent.selected) {         classtype classtype = (classtype) ((jcombobox)e.getsource()).getselecteditem();         if (classtype != null) {             string values[] = classtype.getvalues();         }     }  } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -