2017-02-11 49 views
2

有一個JTable。我想知道它的行數。如何實現這一目標?如何計算JTable的行數?

+1

你爲什麼不讀的javadoc? https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#getRowCount-- –

回答

3

使用getRowCount()方法。 這裏有一個例子:

import javax.swing.JTable; 
public class Main { 
    public static void main(String[] argv) throws Exception { 
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } }; 
    String[] columnNames = { "col1", "col2" }; 

    JTable table = new JTable(cellData, columnNames); 

    int rows = table.getRowCount(); 
    int cols = table.getColumnCount(); 

    System.out.println(rows); 
    System.out.println(cols); 
    } 
} 

參見:http://www.java2s.com/Tutorial/Java/0240__Swing/GettingtheNumberofRowsandColumnsinaJTableComponent.htm