【问题标题】:ArrayList output to JTableArrayList 输出到 JTable
【发布时间】:2014-04-29 01:45:42
【问题描述】:

是的,我用谷歌搜索过,是的,我读过,是的,我仍然卡住了......

我在 ClassB 中有一个对象的 ArrayList,我通过从 B 中的 A 调用一个方法返回到 ClassA 中的对象的 ArrayList。(相同的返回类型:)在 CLassB 中,我完成所有的处理和存储对象属性转换为类类型的对象数组。

B 类:

ClassB object = new ClassB();
ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){
   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  

}

A 类:

ArrayList<ClassB> arrayOfObjects = ClassBObject.method();



String[] columns = {"Column1","Column2","Column3", "Column4", "Column5"};

      DefaultTableModel tableModel = new DefaultTableModel(columns, 0);

      JTable table = new JTable(tableModel);

      int i = 0;
      while(i < arrayOfObjects.size()) {

         //v for variable       
         String v1 = arrayOfObjects .get(i).attribute1;
         String v2 = arrayOfObjects .get(i).attribute2;
         String v3 = arrayOfObjects .get(i).attribute3;
         String v4 = arrayOfObjects .get(i).attribute4;
         String v5 = arrayOfObjects .get(i).attribute5;

         Object[] row = {v1,v2,v3,v4,v5};

         tableModel.addRow(row);
         i++;
      }
         JFrame frame = new JFrame("Title of Table");

            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            JScrollPane tableContainer = new JScrollPane(table);

            panel.add(tableContainer, BorderLayout.CENTER);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

问题:使用上面的代码,表格只显示最后一个对象,该对象重复的次数与 while 循环执行的次数一样多。我想从对象数组中提取每个对象的属性,其中计数器用于匹配对象数组的元素。

我希望我足够详细,可以提供足够的背景知识来找到问题的根源。提前致谢!

【问题讨论】:

  • 您的问题与 JTable 或 Swing 无关,而与您在代码中的某处重用对象或使用静态字段有关。是时候戴上调试帽,寻找罪魁祸首了。
  • 您是否使用 for 循环遍历了数组列表并打印了所有元素?这是基本的调试 101。看起来您的代码中有一个错误未显示。
  • 我的第一个 Java 课程已经 3 个月了,抱歉,如果我没有正确标记它。
  • 不,它与标记无关。你问错问题了。您必须首先隔离您的错误,否则您可能会显示与问题无关的任何内容。我真的不认为您发布的任何代码都是相关的。但是,与其讨论这个,不如现在就进行调试。使用调试器,使用 println,隔离错误。
  • 我在 ClassB 中使用了 println 来构建所有对象。然后我在 ClassA 中使用 println(arrayOfObjects.size()) 来确保它出现。在上面的 while 循环中没有使用 println(JTable 为我做的)我不知道还有哪里可以检查。这就是我发布代码的方式和原因。你还有什么推荐的吗?

标签: java swing arraylist jtable output


【解决方案1】:

好的,现在我们看到了您的问题(可能)!

ClassB object = new ClassB();
ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){
   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  
}

您不会在循环中每次都重新创建一个新对象!

改为尝试:

// ***** don't create object *once* outside of the while loop!
//  ClassB object = new ClassB();

ArrayList<ClassB> arrayOfObjects = new ArrayList<>();
int count = 0;
while(count<10){

   // *** instead create a new one for each iteration of the loop!
   ClassB object = new ClassB(); // *****************

   ///alot going on here but the general concept is this;
  object.attribute1 = "something read in"
  object.attribute2 = "something read in"
  object.attribute3 = "something read in"
  object.attribute4 = "something read in"
  object.attribute5 = "something read in"   

  arrayOfObjects.add(object);
  count++;  
}

【讨论】:

  • 轰隆隆!谢啦!!我真的一直在解决这个晚上的大部分问题!!!!一个很好的作业,我希望我可以分享它,但我需要先对其进行评分,我们承诺不会发布实际代码,如果我们遇到困难,只是使用不同命名法的小样本。谢谢!!!
  • @PadawanDeveloper:不客气。我很高兴这达成了一个愉快的解决方案!
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 2017-09-19
  • 2013-01-23
  • 2013-12-29
  • 2017-07-27
  • 2015-10-31
  • 2013-04-04
  • 2012-01-27
相关资源
最近更新 更多