【问题标题】:Using JComboBox to display in JTable使用JComboBox在JTable中显示
【发布时间】:2012-07-13 06:40:36
【问题描述】:

我试图使用组合框来显示表格中的内容。 我写了下面的代码,但表格中没有显示任何内容。

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);

当我更改如下代码时,它会正确返回数据并显示在表格中。

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);

为什么它不适用于第一个代码 (i) 但适用于第二个代码中的 2?

谁能帮我解决这个问题,我是 Java 和 NetBeans 的新手

【问题讨论】:

  • 调试变量“i”是否返回值。
  • 可以将 Integer 更改为 int 吗? setParameter 可能将 Integer 读取为 Object

标签: java netbeans jtable persistence jdesktop


【解决方案1】:

执行以下操作之一:

  • 调试代码查看i的真实值:

如果 i 为空,那么您需要更改访问组合框所选 id 的方式,如下所示:

Integer i = ((Destination) jComboBox1.getSelectedIndex();
  • 你需要检查他的方法的参数:

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

【讨论】:

    【解决方案2】:

    更改i 的初始化方式:

    int i = jComboBox1.getSelectedIndex();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多