2009-07-05 112 views
0

我是Java新手,我遇到了這樣的問題; 我有一個桌面應用程序,JFrame中有2個jComboBox。這個jComboBox的一個是從Personel Table持有Personels,另一個是Personel的標題。當jComboBox1選擇的索引更改發生時,它將獲得personelid並將其標題填入jComboBox2。所以simple.But選擇指數時改變它的標題填充,但顯示類似Ljava.lang.object.xxxxx ...jCombobox JPA HQL內部連接錯誤

ERROR http://img243.yukle.tc/images/7070error.jpg

這裏是我的代碼;

if (jComboBox1.getSelectedItem() !=null) { 
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SwingDenemePU"); 
    EntityManager em = emf.createEntityManager(); 
    Query sorgu = em.createQuery("from Personel p,Unvan u where p.unvanID = u.unvanID and u.unvanID=:id"); 

int id =((Unvan)jComboBox1.getSelectedItem())。getUnvanID();

sorgu.setParameter("id", id); 

    personelList = sorgu.getResultList(); 

    Object[] items = new Object[personelList.size()]; 

    for (int i = 0; i < personelList.size(); i++) { 
items[i] = personelList.get(i); 
    } 
    DefaultComboBoxModel def = new DefaultComboBoxModel(items); 
    jComboBox2.setModel(def); 

if if change items [i] = personelList.get(i)to;

  Personel personel = personelList.get(i); 
     items[i]=personel.getPersonelAdSoyad(); 

我得到在線程異常 「AWT-EventQueue的-0」 java.lang.ClassCastException:[Ljava.lang.Object;不能轉換爲DBClasses.Personel錯誤。

回答

1

您查詢顯示不正確,不知道你的映射是什麼,但嘗試更多的東西像這樣:

Query sorgu = em.createQuery("select p from Personel p,Unvan u where p.unvanID = u.unvanID and u.unvanID=:id"); 

Query sorgu = em.createQuery("from Personel p where p.unvanID=:id"); 
+0

thanx man你解決了我的問題。 – 2009-07-06 19:46:52

1

默認的組合框渲染器只是調用模型中包含的Object的toString()方法。所以,當你爲模型添加一個字符串時,你會看到字符串的值,因爲那是toString方法返回的值。

如果要存儲在模型中的企業人事的對象,那麼你有兩種選擇:

一)添加toString()方法對企業人事類 B)創建一個自定義渲染器顯示從企業人事的屬性類。

閱讀JComboBox API,您將在「如何使用組合框」中找到Swing教程的鏈接,該教程提供了自定義渲染器的示例。

+0

大家都知道,B是去,總是方式: - ) – kleopatra 2011-09-14 13:47:34