-4

I hava a class StageDAO like this:

public class Stage{

    public int stageID;
    public String label;

    public Stage(ResultSet rs) {
            try{
                this.stageID=rs.getInt("StageID");
                this.label=rs.getString("Label");
            }
            catch(Exception e){}
     }
}

I have a method in a class StageDAO, where i get Data from Database, like this:

public class StageDAO{
Connect connectdb;

public StageDAO(Connect connectdb){
    this.connectdb=connectdb;
}

public Vector retrieveAll() {

    ResultSet lobjRS=null;
    Vector lobjList=new Vector();
    Connection lobjConnection = null;
    Statement lobjStatement=null;        
    Stage lobjStage = null;

    try{
        lobjConnection = this.connectdb.getConnection();
        lobjStatement = lobjConnection.createStatement();
        lobjRS = lobjStatement.executeQuery(
             "SELECT * FROM Stage order by sortkey");
        while(lobjRS.next()){
            lobjStage = new Stage (lobjRS);
            lobjList.add(lobjStage);                
        }
      }catch(){}
   }
}

in my GUi class i have this:

StageDAO lobjStage= new StageDAO (connectdb);
Vector<Stage> stageList = lobjStage.retrieveAll();//Here i have the information 
of stageID  and  stagelabel

private JComboBox lcbstage;

public void initialize(){
    lcbstage= new JComboBox();
    for(int i=0; i<stageList .size();i++){
    lcbstage.addItem(stageList.get(i).label);
    }
}

But know if i select in my Gui the stage, i want to know the stageid. I don't know how to get the stageid of the selected stagelabel ?

Thank you for your help.

3
  • 3
    For better help sooner, post an SSCCE. BTW - }catch(){} That would not even compile. Please stop wasting our time with 'something like' the code used. Jul 10, 2013 at 13:03
  • Ok sorry, i doesnt knew this
    – java java
    Jul 10, 2013 at 13:18
  • 1
    Don't apologize if you don't intend to follow the advice. Jul 10, 2013 at 13:40

1 Answer 1

2

Add the whole item to your JComboBox, not just the label, and create a toString() method which will return just the label:

public String toString(){
    return this.label;
}

JComboBox's addItem takes an object and then converts it to a string for display, so when you do getSelectedItem() the whole object, id and label and everything else, will be returned.

public void initialize(){
    lcbstage= new JComboBox();
    for(int i=0; i<stageList .size();i++){
    lcbstage.addItem(stageList.get(i));//this line changed
    }
}
9
  • thanks, i think you mean like this: lcbstage = new JCombobox(stageList). But know my combobox shows like this: packagename.Stage
    – java java
    Jul 10, 2013 at 13:05
  • Ok i have do like you say. At it works well. But if i want to know the id ... i do lcbstage.getSelectedItem() and i get the label. but i want to have the stageid
    – java java
    Jul 10, 2013 at 13:15
  • To get the I'd, do lcbstage.getSelectedItem().id after putting in the whole item as I do in the above code. Jul 10, 2013 at 13:18
  • To your first comment, did you implement toString like above? Jul 10, 2013 at 13:20
  • I doens't have this option. I have do this like you say. But i can only use the string operations.
    – java java
    Jul 10, 2013 at 13:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.