JTable不返回所有记录的数据

时间:2023-01-27 13:22:59

I am using a JTable and selecting all the rows by CTRL+A. I am getting the selected rows correct but when I am trying to get the values it only gives the values from viewport.

我正在使用JTable并通过CTRL + A选择所有行。我正在使所选行正确,但是当我尝试获取值时,它只提供来自视口的值。

I am adding a part of my code here.

我在这里添加了部分代码。

JTable _resultTable = new JTable();
JScrollPane _resultPane = new JScrollPane();

_resultTable.setModel(JUTableBindingFactory.createAttributeListBinding(_panelBinding, _resultTable , _searchViewName, _searchViewName + "Iter", _searchViewName + "IterBinding", FIELDS));
_resultPane.getViewport().add(_resultTable);
MultiSelectionListListener.setMultiSelectionModel(_resultTable);

_changeSelectedButton.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
    int[] tableRows = _resultTable.getSelectedRows();

    System.out.println(" length :" + tableRows.length);

    Row[] dataRows = new Row[tableRows.length];

    RowIterator iterator = null;
    JUIteratorBinding iterBinding = _panelBinding.getRowIterBinding(_searchViewName, _searchViewName + "Iter",_searchViewName.replace('.','_')+"IterBinding");
    iterator = iterBinding != null ? iterBinding.getNavigatableRowIterator() : null;

    for (int i = 0; i < tableRows.length; i++)
    {
      dataRows[i] = iterator.getRowAtRangeIndex(rowIndexToRangeIndex(tableRows[i], iterator));
      System.out.println(" Name :" + i + " " +dataRows[i].getAttribute(1));
    }
  }

From above code snippet if the number of records present are 50 and we can see 10 records, then the output is somehow :

从上面的代码片段中,如果存在的记录数是50,我们可以看到10条记录,那么输出是以某种方式:

length :50

Name: 0 Nadine

名称:0 Nadine

Name : 1 Nadine1

名称:1 Nadine1

Name : 2 Nadine23

姓名:2 Nadine23

Name : 3 Nadine3

名称:3 Nadine3

Name : 4 Nadine4

名称:4 Nadine4

Name : 5 Nadine5

名称:5 Nadine5

Name : 6 Nadine6

姓名:6 Nadine6

Name : 7 Nadine7

姓名:7 Nadine7

Name : 8 Nadine8

姓名:8 Nadine8

Name : 9 Nadine9

姓名:9 Nadine9

Name : 10 Nadine10

名称:10 Nadine10

And after printing 10 records it gives NullPointerExpetion at dataRows[i].getAttribute(1) .

打印10条记录后,它会在dataRows [i] .getAttribute(1)中给出NullPointerExpetion。

So that means it is not picking the data for the records that are not on the viewport.

这意味着它不会为不在视口上的记录选择数据。

So what could be the solution ?

那可能是什么解决方案呢?

EDIT I am using BC4J binding to get the data in the table.

编辑我使用BC4J绑定来获取表中的数据。

2 个解决方案

#1


2  

Some things suggested by your snippet:

您的代码段建议的一些内容:

  • Don't mix model and view coordinates, convert them as required.

    不要混合模型和视图坐标,根据需要进行转换。

  • Access your TableModel, not the data structure that was used to construct it.

    访问TableModel,而不是用于构造它的数据结构。

#2


0  

I have searched the issue on internet and found a link which helps me to understand the issue. The data was not being loaded because the binding loads that much data which needs to be shown on the viewport. So while trying to access the data which was not on viewport was giving error.

我在互联网上搜索了这个问题,找到了一个帮助我理解这个问题的链接。数据未加载,因为绑定会加载需要在视口上显示的大量数据。因此,在尝试访问不在视口上的数据时,却给出了错误。

To solve the issue we just need to load all the data in View Object by tweaking the bindings a bit.

要解决这个问题,我们只需稍微调整绑定就可以加载View Object中的所有数据。

From

 JUIteratorBinding iterBinding = _panelBinding.getRowIterBinding(_searchViewName, _searchViewName + "Iter",_searchViewName.replace('.','_')+"IterBinding");

To

DCIteratorBinding iterBinding = _panelBinding.getIteratorBinding(_searchViewName, _searchViewName + "Iter",_searchViewName.replace('.','_')+"IterBinding", -1);

The last argument -1 is used to setRangeSize(-1), which will load all data.

最后一个参数-1用于setRangeSize(-1),它将加载所有数据。

#1


2  

Some things suggested by your snippet:

您的代码段建议的一些内容:

  • Don't mix model and view coordinates, convert them as required.

    不要混合模型和视图坐标,根据需要进行转换。

  • Access your TableModel, not the data structure that was used to construct it.

    访问TableModel,而不是用于构造它的数据结构。

#2


0  

I have searched the issue on internet and found a link which helps me to understand the issue. The data was not being loaded because the binding loads that much data which needs to be shown on the viewport. So while trying to access the data which was not on viewport was giving error.

我在互联网上搜索了这个问题,找到了一个帮助我理解这个问题的链接。数据未加载,因为绑定会加载需要在视口上显示的大量数据。因此,在尝试访问不在视口上的数据时,却给出了错误。

To solve the issue we just need to load all the data in View Object by tweaking the bindings a bit.

要解决这个问题,我们只需稍微调整绑定就可以加载View Object中的所有数据。

From

 JUIteratorBinding iterBinding = _panelBinding.getRowIterBinding(_searchViewName, _searchViewName + "Iter",_searchViewName.replace('.','_')+"IterBinding");

To

DCIteratorBinding iterBinding = _panelBinding.getIteratorBinding(_searchViewName, _searchViewName + "Iter",_searchViewName.replace('.','_')+"IterBinding", -1);

The last argument -1 is used to setRangeSize(-1), which will load all data.

最后一个参数-1用于setRangeSize(-1),它将加载所有数据。