简体   繁体   中英

Using JComboBox to display in JTable

I was trying to use a combobox to show contents in a table. I wrote the code below, bu nothing displayed in the table.

Integer i = ((Destination) (jComboBox1.getSelectedItem())).getId();
query1 = entityManager.createQuery("SELECT d FROM Dayactivity d WHERE d.id=:Id");
query1.setParameter("Id", i);
java.util.Collection data = query1.getResultList();
list2.clear();
list2.addAll(data);

When I changed the code as below it returned data correctly and displayed in the table.

Integer i = ((Destination) (jComboBox1.getSelectedItem())).getId();
query1 = entityManager.createQuery("SELECT d FROM Dayactivity d WHERE d.id=:Id");
query1.setParameter("Id", 2);
java.util.Collection data = query1.getResultList();
list2.clear();
list2.addAll(data);

Why It is not working for the first code (i) but works for 2 in second code?

Can somebody help me to solve this, I am new to Java & NetBeans

i的初始化方式更改为:

int i = jComboBox1.getSelectedIndex();

Do one of the following:

  • Debug the code to see the real value of i:

if i is null then you need to change how you access the selected id of combobox as follows:

Integer i = ((Destination) jComboBox1.getSelectedIndex();
  • You need to check the argument of his method:

    1. query1.setParameter(String, int); or
    2. query1.setParameter(String, Integer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM