简体   繁体   English

在第一行中设置默认值

[英]Making a default value in a first row

在此处输入图片说明

I have this code that add row depends on the user input, but now I want the first row of the second column to have a default value of 0 and the rest of it is null. 我有这段代码,添加行取决于用户输入,但是现在我希望第二列的第一行的默认值为0,其余的为null。

Code of the table 表代码

DefaultTableModel dm = new DefaultTableModel();

 table.setModel(dm);


            dm.addColumn("PROCESS");
            dm.addColumn("CPU Time");
            dm.addColumn("Arrival Time");


 try{
        int ps = Integer.parseInt(textField.getText());



        for(int x=0; x<ps; x++){
            dm.addRow(new Object[]{x});
        }
        }catch(Exception e){
            e.printStackTrace();
        }   
    }

You can call addRow twice with different arguments: first adding the row with 0 , and then (in a loop) adding the remaining rows: 您可以使用不同的参数调用addRow两次:首先将行添加0 ,然后(在循环中)添加其余行:

dm.addRow(new Object[]{x, 0});
for (int x=1; x<ps; x++) {
    dm.addRow(new Object[]{x});
}

Notice that the loop is now one iteration shorter than in your code (because the first row gets added outside the loop). 请注意,循环现在比您的代码短了一个迭代(因为第一行被添加到循环外部)。

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

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