Get row by index from JTable

后端 未结 7 675
情歌与酒
情歌与酒 2021-01-07 10:32

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

相关标签:
7条回答
  • 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);
    
    0 讨论(0)
  • 2021-01-07 10:59

    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

    0 讨论(0)
  • 2021-01-07 11:02

    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.

    0 讨论(0)
  • 2021-01-07 11:04

    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.

    0 讨论(0)
  • 2021-01-07 11:17

    Try something like this

    private void getIndexRow(){
        int i;
        int row = 0;
        int column = 0;
        i=Integer.parseInt(myTable.getValueAt(row,column).toString());
    }
    
    0 讨论(0)
  • 2021-01-07 11:18

    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;
    }
    
    0 讨论(0)
提交回复
热议问题