简体   繁体   English

在一个循环中将行和列添加到jTable

[英]add row and column to jTable in one loop

I am working in NetBeans IDE, language Java, the main class is JFrameForm. 我正在使用Java语言NetBeans IDE,主要类是JFrameForm。

I have a jTable tab with just one row and one column, button and jTextField en , where type should be integers. 我有只有一行和一列,按钮的JTextField ,这里的类型应该是一个整数JTable的标签 The input is variable n . 输入是变量n

I need to create matrix with n rows and n columns. 我需要创建具有n行和n列的矩阵。 So n x n dimension of matrix as a jTable. 因此矩阵的n x n维为jTable。

After click on the button, variable n will be saved as dimension and loop will start add the column and row till n . 单击按钮后,变量n将保存为维度,循环将开始添加列和行,直到n为止。

The code is following: 代码如下:

private void sendMouseClicked(java.awt.event.MouseEvent evt) {                                    
        DefaultTableModel model = (DefaultTableModel) tab.getModel();

        String sn=en.getText();
        int n=Integer.valueOf(sn);

        for(int j=2;j<=n;j++){
            model.addColumn(null); // I know this is wrong
            model.addRow(new Object[]{test.getText()+j});
            test.setText(test.getText()+j);
        }
    }         

I got error 我有错误

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 线程“ AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:1

The cells should be empty. 单元格应该为空。

Please help me to input the column. 请帮助我输入该列。 What is object there? 那里有什么物体?

set column names to JTable and then add rows in JTable.. 将列名称设置为JTable,然后在JTable中添加行。

private void sendMouseClicked(java.awt.event.MouseEvent evt) {                                    
    String sn=en.getText();
    int n=Integer.valueOf(sn);
    java.util.Vector columns = new java.util.Vector();
    columns.add("Your Column Name");
    java.util.Vector rows = new java.util.Vector();
    for(int j=2;j<=n;j++){
        java.util.Vector row = new java.util.Vector();
        row.add(test.getText()+j);
        rows.add(row);
        test.setText(test.getText()+j);
    }
    DefaultTableModel model = new DefaultTableModel(rows, columns);
    tab.setModel(model);
}

this will work.. 这会工作..

I think (I didn't checked it) that your JTable tries to add a row but it doesn't have any column because of your addColumn(null) . 我认为(我没有检查过)您的JTable尝试添加一行,但是由于您的addColumn(null)它没有任何列。

Why don't you just do model.addColumn(""); 你为什么不做model.addColumn(""); with an empty string to add an empty cell ? 用空字符串添加一个空单元格?

From what I can deduce, you want to use the variable n as an int. 根据我的推断,您想将变量n用作整数。 The getText() method will return the value as a string and the valueOf() method will return the string as a string. getText()方法将以字符串形式返回该值,而valueOf()方法将以字符串形式返回该字符串。 valueOf() is used for the exact opposite of what you want, converting an int for example into a string. valueOf()用于您想要的相反 ,例如将int转换为字符串。 You should use Integer.parseInt() instead outlined in this Stackoverflow question . 您应该使用Integer.parseInt()代替此Stackoverflow问题中概述的内容。 That will hopefully get rid of the out of bound exceptions. 希望它将摆脱无限的例外。

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

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