从 JTable 中按索引获取行

发布于 2024-10-28 22:22:37 字数 66 浏览 1 评论 0原文

如何从 JTable 获取索引为 i 的行?我查看了成员函数,但没有像 getRowAt 这样的函数。有人可以帮忙吗?

How to get row with index i froj JTable ? I looked at member functions but there is nothing like getRowAt . Can anybody help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

肤浅与狂妄 2024-11-04 22:22:37

表没有“行”对象,因此使用 getRow 方法无法获得任何结果。

您可以要求 getValueAt() 获取各个值,将其用于每一列,然后您就获得了完整的行。

There is no "row" object for a table, so nothing you could get with a getRow method.

You can ask getValueAt() to get the individual values, use it for each column and you have your complete row.

半世蒼涼 2024-11-04 22:22:37

AFAIK,没有这样的方法。编写类似的内容:

public String[] getRowAt(int row) {
     String[] result = new String[colNumber];

     for (int i = 0; i < colNumber; i++) {
         result[i] = table.getModel().getValueAt(row, col);
     }

     return result;
}

PS - 如果您想尊重用户列顺序重新排列,请使用 table.getValueAt()

AFAIK, there is no such method. Write something like that:

public String[] getRowAt(int row) {
     String[] result = new String[colNumber];

     for (int i = 0; i < colNumber; i++) {
         result[i] = table.getModel().getValueAt(row, col);
     }

     return result;
}

P.S - Use table.getValueAt() if you want to respect a rearranged by the user column order.

同尘 2024-11-04 22:22:37

我建议基于 POJO 列表创建一个 TableModel。

然后很容易添加一个方法,例如:

   MyPojo getData(int index);

看看我不久前写的这个示例作为起点:
http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample /src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

I recommend to create a TableModel based on a list of POJOs.

It's then easy to add a method like:

   MyPojo getData(int index);

Have a look at this sample I wrote some time ago for a starting point:
http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

无畏 2024-11-04 22:22:37

尝试这样的事情

private void getIndexRow(){
    int i;
    int row = 0;
    int column = 0;
    i=Integer.parseInt(myTable.getValueAt(row,column).toString());
}

Try something like this

private void getIndexRow(){
    int i;
    int row = 0;
    int column = 0;
    i=Integer.parseInt(myTable.getValueAt(row,column).toString());
}
遗心遗梦遗幸福 2024-11-04 22:22:37

另一种方法是使用表模型的 getDataVector() 方法。

DefaultTableModel tm = (DefaultTableModel) table.getModel();
Vector<Object> rowData = tm.getDataVector().elementAt(rowIndex);

Another way of doing it is using the table model's getDataVector() method.

DefaultTableModel tm = (DefaultTableModel) table.getModel();
Vector<Object> rowData = tm.getDataVector().elementAt(rowIndex);
仙气飘飘 2024-11-04 22:22:37
 private void jTable1MousePressed(java.awt.event.MouseEvent evt) {                                     

    int selectedRow;

    ListSelectionModel rowSM = jTable1.getSelectionModel();

      rowSM.addListSelectionListener(new ListSelectionListener()
      {
        @Override
        public void valueChanged(ListSelectionEvent e) 
        {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            selectedRow = lsm.getMinSelectionIndex();

            int numCols = jTable1.getColumnCount();

            model = (DefaultTableModel) jTable1.getModel();

            System.out.print(" \n row " + selectedRow + ":");

            for (int j = 0; j < numCols; j++) 
            {
                System.out.print(" " + model.getValueAt(selectedRow, j));
            }

        }
    });
}

使用此功能,您可以获取单击特定行时整行的值。

 private void jTable1MousePressed(java.awt.event.MouseEvent evt) {                                     

    int selectedRow;

    ListSelectionModel rowSM = jTable1.getSelectionModel();

      rowSM.addListSelectionListener(new ListSelectionListener()
      {
        @Override
        public void valueChanged(ListSelectionEvent e) 
        {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            selectedRow = lsm.getMinSelectionIndex();

            int numCols = jTable1.getColumnCount();

            model = (DefaultTableModel) jTable1.getModel();

            System.out.print(" \n row " + selectedRow + ":");

            for (int j = 0; j < numCols; j++) 
            {
                System.out.print(" " + model.getValueAt(selectedRow, j));
            }

        }
    });
}

Using this you can get value of whole row where u click on particular row.

在风中等你 2024-11-04 22:22:37

这个功能对我来说效果很好。

private Object[] getRowAt(int row, DefaultTableModel model) {
    Object[] result = new Object[model.getColumnCount()];

     for (int i = 0; i < model.getColumnCount(); i++) {
         result[i] = model.getValueAt(row, i);
     }

     return result;
}

This function is working well for me.

private Object[] getRowAt(int row, DefaultTableModel model) {
    Object[] result = new Object[model.getColumnCount()];

     for (int i = 0; i < model.getColumnCount(); i++) {
         result[i] = model.getValueAt(row, i);
     }

     return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文