简体   繁体   English

如何从 JTable 中获取数据?

[英]How to get data from a JTable?

OK, I want to get all the data from the first column of a JTable.好的,我想从 JTable 的第一列中获取所有数据。 I though the best way would be pulling it in to a ArrayList , so I made one.我认为最好的方法是将它拉入ArrayList ,所以我做了一个。 I also made an instance of a TableModel :我还制作了一个TableModel的实例:

static DefaultTableModel model = new javax.swing.table.DefaultTableModel(); 
f.data.setModel(model); //f.data is the JTable

public static final void CalculateTotal(){
    ArrayList<String> numdata = new ArrayList<String>();

  for(int count = 1; count <= model.getRowCount(); count++){
      numdata.add(model.getValueAt(count, 1).toString());

  }
  System.out.println(numdata); 
}

This gives me a NullPointerException (cue screams).这给了我一个 NullPointerException(提示尖叫)。 What am i doing wrong?我究竟做错了什么?

I don't know those classes well, but I would guess you'll have to count from zero:我不太了解这些课程,但我猜你必须从零开始计数:

for (int count = 0; count < model.getRowCount(); count++){
  numdata.add(model.getValueAt(count, 0).toString());
}

In Java, it is usual to count from 0 (like in most C-like languages)...在 Java 中,通常从 0 开始计数(就像在大多数类 C 语言中一样)...

It is best if you could post SSCCE that shows model initialization and its population with data.最好是您可以发布显示模型初始化及其填充数据的SSCCE Also include details of the exception as there could be multiple sources for the problem.还包括异常的详细信息,因为问题可能有多个来源。

Here is a demo based on @CedricReichenbach correction:这是一个基于@CedricReichenbach 校正的演示:

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

public class TestModel {
    public static void main(String s[]) {
        DefaultTableModel model = new javax.swing.table.DefaultTableModel();    

        model.addColumn("Col1");
        model.addColumn("Col2");

        model.addRow(new Object[]{"1", "v2"});
        model.addRow(new Object[]{"2", "v2"});

        List<String> numdata = new ArrayList<String>();
        for (int count = 0; count < model.getRowCount(); count++){
              numdata.add(model.getValueAt(count, 0).toString());
        }

        System.out.println(numdata); 
    }
}

The result is:结果是:

[1, 2]

I know this answer is a bit late, but it is actually a very easy problem to solve.我知道这个答案有点晚了,但这实际上是一个很容易解决的问题。 Your code gives an error when reading the entry because there is no entry in the table itself for the code to read.您的代码在读取条目时出错,因为表本身没有条目供代码读取。 Populate the table and run your code again.填充表格并再次运行您的代码。 The problem could have been solved earlier but from the code you posted it was not obvious what was inside your table.这个问题本可以早点解决,但是从您发布的代码来看,您的表中的内容并不明显。

       DefaultTableModel model = (DefaultTableModel) table.getModel();

        for (int i = 0; model.getRowCount() > i; i++) {
            final String col1 = (String) model.getValueAt(i, 0);
            final String col2 = (String) model.getValueAt(i, 1);

            System.out.println("Row: " + i + " Col 0: " + col1);
            System.out.println("Row: " + i + " Col 1: " + col2);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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