2009-07-05 111 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