Question

I want to call course name in combobox and print course Id which selected coursename How can I solve this problem?

    public void coursename(){
     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
     session.beginTransaction();
     Query query= session.createQuery("select a.courseName,a.courseId  from Semester e inner join e.course as a"); 
   for (Iterator it = query.iterate(); it.hasNext();) {
      Object  row[] = (Object[])   it.next();
      combocourse.addItem(row[0]);
        }        
       session.close();
   }


    private void combocourseActionPerformed(java.awt.event.ActionEvent evt) {                                            


  JComboBox combocourse = (JComboBox)evt.getSource();  
   Object row[] = (Object[])combocourse.getSelectedItem();  
    System.out.println("id"+row[1] ); 

       }
Was it helpful?

Solution

By not trying to cast a String to an Object[]. Look at the return value of the methods you're using, and use variables typed appropriately to store those return values. JComboBox#getSelectedItem returns an Object (in this case apparently a String), not an array (of any kind). But in this line:

Object row[] = (Object[])combocourse.getSelectedItem();

...you're trying to cast it to be an Object[] (array of Object) so you can store it in an Object[]. You can't do that.

It seems like row should just be Object or String, not Object[], and that when you use it you should just use it directly, not as row[1]:

Object row = combocourse.getSelectedItem();  
System.out.println("id"+row ); 

Or

String row = (String)combocourse.getSelectedItem();  
System.out.println("id"+row ); 

In a comment you asked:

I called coursename in combobox but i should save course id in my database. How can I get courseId?

I don't know JComboBox. Fundamentally, you need to store something that contains both values (the ID and name) and then use that something when you get the selected item. Unless JComboBox has some functionality for this built in, you might need a simple class for that that holds the values and implements toString by returning courseName. Something vaguely like:

class CourseItem {
    private String courseName;
    private String courseId; // Or int or whatever

    CourseItem(String courseName,String courseId) {
        this.courseName = courseName;
        this.courseId = courseId;
    }

    public String getCourseName() {
        return this.courseName;
    }

    public String getCourseId() {
        return this.courseId;
    }

    public String toString() { // For display
        return this.courseName;
    }
}

Then:

public void coursename() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery("select a.courseName,a.courseId  from Semester e inner join e.course as a");
    for (Iterator it = query.iterate(); it.hasNext();) {
        Object row[] = (Object[]) it.next();
        combocourse.addItem(new CourseItem((String)row[0], (String)row[1]));
    }
    session.close();
}

private void combocourseActionPerformed(java.awt.event.ActionEvent evt) {


    JComboBox combocourse = (JComboBox) evt.getSource();
    CourseItem item = (CourseItem)combocourse.getSelectedItem();
    System.out.println("id" + item.getCourseId());

}

OTHER TIPS

try:

Object row = (Object)combocourse.getSelectedItem();  
System.out.println("id"+row ); 

You only add single objects into the combocourse, not arrays of objects.

combocourse.getSelectedItem(); in your case returns String and string cannot be cast to array of objects. If you want to get List of Objects, they use getSelectedObjects()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top